473,377 Members | 1,153 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

Static method vs Non Static method

i really wander what makes static method special?

in fact i can access a non static method statically using '::'

class aclass {
function anonstatic() {
echo 'non static';
}

static function astatic() {
echo 'static';
}
}

$aobj = new aclass();

aclass::anonstatic();
//it works.

$aobj->astatic();
//it works too.

i see a diffrence that i can't use $this in static method but nothing.

i don't feel there's no reason i have to make a mothod static.

is there any reason i have to declare a mothod as static?

apache2.0/php5.14

May 26 '06 #1
6 31706
gg9h0st said the following on 26/05/2006 09:36:
i really wander what makes static method special?

in fact i can access a non static method statically using '::'

class aclass {
function anonstatic() {
echo 'non static';
}

static function astatic() {
echo 'static';
}
}

$aobj = new aclass();

aclass::anonstatic();
//it works.

$aobj->astatic();
//it works too.

i see a diffrence that i can't use $this in static method but nothing.

i don't feel there's no reason i have to make a mothod static.

is there any reason i have to declare a mothod as static?


Static methods and variables are useful when you want to share
information between objects of a class, or want to represent something
that's related to the class itself, not any particular object.

For some reason, PHP allows you to call non-static and static methods
interchangeably, which as far as I can see, makes *no* sense at all,
neither from a semantic nor a programmatic point of view.
--
Oli
May 26 '06 #2
gg9h0st wrote:
i really wander what makes static method special?


Nothing. It's mainly for documentation purpose. I mean, you don't
really need to declare class variable either. IIRC static methods will
show up in a separate section when class info is retrieved through the
reflection API.

Often times in PHP you can do funky things simply because it's not
worth the effort to stop funk.

May 26 '06 #3
Chung Leong said the following on 26/05/2006 17:05:
gg9h0st wrote:
i really wander what makes static method special?


Nothing. It's mainly for documentation purpose. I mean, you don't
really need to declare class variable either. IIRC static methods will
show up in a separate section when class info is retrieved through the
reflection API.

Often times in PHP you can do funky things simply because it's not
worth the effort to stop funk.


If I understand your allusion correctly, in some ways, I'd say it's the
other way round. PHP lets you do a lot of unfunky things because the
developers didn't like the idea of being seen to enforce "funkiness" on
those uninitiated in the ways of funkiness, even though disciplined
funkiness would have been for their own good! IMO, the
static/non-static confusion and ability to add object variables
willy-nilly are two perfect examples of this in PHP.
Static members mean a lot more than that just a neat way of accessing
them separately via reflection, it's just that PHP has managed to
confuse the issue by letting the programmer do weird things for no good
reason. "Stricter" OO languages such as C++, Java, and C# that have the
concept of static members enforce them as such (being compiled
languages, they have to, obviously), and so they become a useful
"semantic" design tool, in the same way that inheritance, "abstract",
"final" and visibility specifiers are all useful, but completely
unnecessary in terms of program functionality.
--
Oli
May 26 '06 #4
Oli Filth wrote:
If I understand your allusion correctly, in some ways, I'd say it's the
other way round. PHP lets you do a lot of unfunky things because the
developers didn't like the idea of being seen to enforce "funkiness" on
those uninitiated in the ways of funkiness, even though disciplined
funkiness would have been for their own good! IMO, the
static/non-static confusion and ability to add object variables
willy-nilly are two perfect examples of this in PHP.
I guess "funk" is not a very precise term :-)

What I meant was the ability to call a non-static function statically
is highly unorthodox, but it requires extra effort to stop it. The
problem is here that the class::method() syntax is overloaded: it's
used for calling static method as well as for a non-static method to
call an overridden non-static methods in the ancestor classes. To
determine if a invocation is "legal" requires checking the context in
which it was made, which has to happen at runtime, with every call.

Whether it's worth the CPU cycles to do the check is debatable. But
since the aggregation API relies on this quirk, you can't really get
rid of it now.
Static members mean a lot more than that just a neat way of accessing
them separately via reflection, it's just that PHP has managed to
confuse the issue by letting the programmer do weird things for no good
reason. "Stricter" OO languages such as C++, Java, and C# that have the
concept of static members enforce them as such (being compiled
languages, they have to, obviously), and so they become a useful
"semantic" design tool, in the same way that inheritance, "abstract",
"final" and visibility specifiers are all useful, but completely
unnecessary in terms of program functionality.


If you're saying that PHP isn't a good OO language, I wouldn't
disagree. In my opinion a lot of the additions in PHP 5 are just me-too
fluff. PHP can never be a strict language because of its dynamic
nature. When you can have constructs like this:

if($best_case_scenario) {

class Iraq extends Democracy {
public $army;
}

} else {

class Iraq extends Quagmire {
private $army;
}

}

The kinds of checks you can do at compile time are very limited. Doing
validation at runtime on the other hand is a drag on performance, which
is already low because the language is interpreted.

May 26 '06 #5
Chung Leong said the following on 26/05/2006 18:48:
Oli Filth wrote:
If I understand your allusion correctly, in some ways, I'd say it's the
other way round. PHP lets you do a lot of unfunky things because the
developers didn't like the idea of being seen to enforce "funkiness" on
those uninitiated in the ways of funkiness, even though disciplined
funkiness would have been for their own good! IMO, the
static/non-static confusion and ability to add object variables
willy-nilly are two perfect examples of this in PHP.
I guess "funk" is not a very precise term :-)


Yes, it looks like I completely misinterpreted what you meant!

What I meant was the ability to call a non-static function statically
is highly unorthodox, but it requires extra effort to stop it. The
problem is here that the class::method() syntax is overloaded: it's
used for calling static method as well as for a non-static method to
call an overridden non-static methods in the ancestor classes. To
determine if a invocation is "legal" requires checking the context in
which it was made, which has to happen at runtime, with every call.
Hmm, I'd never thought about it like that. You're right of course.
However, this logic applies to any use of a function/member/method at
any point. Any time a symbol is used, PHP presumably has to interrogate
some kind of symbol table to find whether that symbol exists in the
current scope, and is legal.
<...SNIP MY POLEMIC...>
If you're saying that PHP isn't a good OO language, I wouldn't
disagree. In my opinion a lot of the additions in PHP 5 are just me-too
fluff. PHP can never be a strict language because of its dynamic
nature. When you can have constructs like this:

if($best_case_scenario) {

class Iraq extends Democracy {
public $army;
}

} else {

class Iraq extends Quagmire {
private $army;
}

}
Political connotations aside ( :) ), it's this kind of nonsense that
makes me never want to use PHP again, at times! I know that some people
see this as a useful "feature", but coming from a compiled-language
background, I see this only as a development nightmare, and a massive
limitation on language strictness, as you've already commented on...
(Just in case; yes, I know that PHP isn't the only language that does this.)

The kinds of checks you can do at compile time are very limited. Doing
validation at runtime on the other hand is a drag on performance, which
is already low because the language is interpreted.

--
Oli
May 26 '06 #6
Oli Filth wrote:
Hmm, I'd never thought about it like that. You're right of course.
However, this logic applies to any use of a function/member/method at
any point. Any time a symbol is used, PHP presumably has to interrogate
some kind of symbol table to find whether that symbol exists in the
current scope, and is legal.
Well, you would have to travel up the hierarchy to see if $this is
descended from the class. Not terribly expensive, but sort of pointless
given that the code would blow out anyway as soon as $this is accessed.
And if there's a check, you can't use such nice construct as this:

class A {
private $a = "Hello world";

public function Test() {
echo $this->a;
}
}

class B {
public $a = "The end is near";

public function Test() {
A::Test();
}
}

$b = new B();
$b->Test();

Totally unorthodox, but useful in some situations.
Political connotations aside ( :) ), it's this kind of nonsense that
makes me never want to use PHP again, at times! I know that some people
see this as a useful "feature", but coming from a compiled-language
background, I see this only as a development nightmare, and a massive
limitation on language strictness, as you've already commented on...
(Just in case; yes, I know that PHP isn't the only language that does this.)


One has to make a distinction between different environments though.
With PHP, you can very quickly see the result of a script. If there's a
bug in the code, it's usually quite obvious. You don't really need
warning from the compiler as much compared to a large C++ program.

May 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: javac | last post by:
to learn java I'm working on an eco-system simulation, the source's at: http://www.geocities.com/cjavacjava/src/ class XYZcoordinates has three fields: int x,y,z class Cat inherits from...
3
by: Dave | last post by:
Hi, Is there a general rule to use 'static' on a class member? It seems uneccessary to have to create an instance of an object just to use it's methods where declaring something as static makes...
2
by: Jason Huang | last post by:
Hi, What's the difference "public static" and "static"? Do we have "protected static" and "private static"? If we do, in what situation will we apply them? Thanks for help. Jason
19
by: shana07 | last post by:
Good Day All! I have one problem and wish to consult...... I am using one program -mujava to create mutants. I run the program and comes out this: MyProgram.java class containts 'static void...
5
by: Jim Langston | last post by:
The output of the following program for me is: Same 0 1 2 Which is what I want. I just want to confirm this is well defined behavior, that a static local variable to a class function/method is...
4
by: Steffen Bobek | last post by:
Extension methods are made for use with instances. I'd like to "misuse" them as static methods, too. Let me tell you my ambition: I use an extension method to serialize objects somehow like this:...
4
by: Jon Skeet [C# MVP] | last post by:
On Aug 11, 5:11 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote: Exactly. My favourite example is Thread.Sleep. Suppose this code were valid (its equivalent in Java is, for example): ...
1
by: prisesh26 | last post by:
What is the difference between declaring a member variable as final static and static final? thanks
2
by: Andrus | last post by:
Winforms UI assembly has static FormManager.FormCreator method which creates forms taking entity as parameter. I need to pass this method to business objects in business assembly so that business...
8
by: Jimmysevens | last post by:
Hi there, I'm new to java and would appreciate help with this. My project involves me creating 5 classes; Person, Employee, Demonstrator, Payroll and Test. Employee and demonstrator are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.