473,562 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::anonsta tic();
//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 31725
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::anonsta tic();
//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_s cenario) {

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_s cenario) {

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
2046
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 abstract class LifeForm each cat needs one and only one XYZcoordinates object I think I know what I want, but keep getting errors about
3
22376
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 more sense. Thanks
2
1417
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
2753
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 main() method. Please note that mutants are not generated for the 'static void main()' method. How to solve this problem or how am I supposed to...
5
1750
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 the same instance across classes. #include <iostream> #include <vector>
4
3351
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: MyObject obj = new MyObject(); obj.ToXmlFile("some directory\\some file.xml"); The method "ToXmlFile" is defined elsewhere far apart from...
4
1907
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): ThreadStart ts = SomeMethodToRun; Thread thread = new Thread(ts); thread.Start(); thread.Sleep(500);
1
1562
by: prisesh26 | last post by:
What is the difference between declaring a member variable as final static and static final? thanks
2
2457
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 methods can also create forms but does not have reference to UI assembly. I tried code below but got compile errows shown in comments. How to fix...
8
2765
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 inherited from person. Data is read in from 2 seperate text files for each of these. Test contains the main() method Payroll contains an array of...
0
7658
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7630
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6228
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5479
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5198
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3609
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.