php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40886 static methods assigned to instances
Submitted: 2007-03-21 18:56 UTC Modified: 2007-03-23 09:55 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: andrea at 3site dot it Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.1 OS: Windows XP SP2
Private report: No CVE-ID: None
 [2007-03-21 18:56 UTC] andrea at 3site dot it
Description:
------------
Description:
------------
I don't know if it is by design, but this is not what I would expect
logically ... (and with static variables it doesn't happen so it should be a _strange_ logic)

I suppose this problem is related with this one:
http://bugs.php.net/bug.php?id=40837

but I think this one is *not* callable Irrelevant

Reproduce code:
---------------
<?php
	class ExampleClass {
	
		public static function StaticExample(){
			echo "StaticExample", "<br />";
		}
	
		public function InstanceExample(){
			echo "InstanceExample", "<br />";
		}
	}

	$test = new ExampleClass();
	ExampleClass::StaticExample();	// ok
	$test->InstanceExample();	// ok
	$test->StaticExample();		// what the hell?
?>

Expected result:
----------------
StaticExample
InstanceExample
FATAL ERROR ... undefined method StaticExample

Actual result:
--------------
StaticExample
InstanceExample
StaticExample

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-21 20:13 UTC] andrea at 3site dot it
damn ... http://www.php.net/manual/en/language.oop5.static.php

"Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can)."

Well ... C# and other languages doesn't assign static methods to instances.

C++ does it but it assign static parameters too.

With PHP 5 we can't use the same name for 2 different methods (for example one static and one public) but we can call a static method without static declaration (only E_STRICT tells us there's something wrong) while C++ can't call a public method, or parameter, with a class if it's not declared as static.

At this point, why did You introduce the static method/property type?
This implementation is not Object Oriented, it's quite "Hilarius" Oriented.

Sorry for this bug (and for me it's really a bug!).
Regards.
 [2007-03-21 21:02 UTC] tony2001@php.net
Expected behaviour.
 [2007-03-22 11:19 UTC] andrea at 3site dot it
This cannot be an expected behaviour because in this way a static method is exactly the same of a generic public method.

Static parameters aren't (correctly) usable with instances so why static methods should be assigned?

If this is an expected behaviour please tell us what do You think static keyword means and explain them correctly on documentation page.
 [2007-03-22 11:29 UTC] daniele_dll at yahoo dot it
Hi,

i was talking with andrea yesterday evening and he was explaining me that stuff.

I don't know if it is an expected behaviour or not, but i'm sure that somewhere there is a problem!

Infact, if it is an expected behaviour the static keyword loss it meanings and, probably, slowdown the php page compilation/execution, but if it is normal documentation should be fixed because it says a totally different stuff.

However, to get back to the problem, the manual says, as should be:
"A member declared as static can not be accessed with an instantiated class object"

Because is a non sense say that something is static and after let to the code to call it as non static
 [2007-03-22 12:15 UTC] derick@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.

 [2007-03-22 12:18 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2007-03-22 12:28 UTC] andrea at 3site dot it
Derick, this is a bug (any Object Oriented logic).
There's something wrong in your static keyword implementation, at least for methods that uses static keyword.

This my last call and this is my last example:

<?php
class ExampleClass {

	public	$StaticExample;

	public final function __construct(){
		// bye bye public *parameter*
		$this->StaticExample = create_function('$never', 'return "welcome PHP5 ambiguity";');
	}
	
	public final static function StaticExample(){
		echo "StaticExample", "<br />";
	}
}

$test = new ExampleClass();
ExampleClass::StaticExample();
exit($test->StaticExample());
?>

What does static keyword mean for PHP 5 developers?
If this is an expected behaviour you should explain them in documentation page.

Regards (I'll never open again this *bug*)
 [2007-03-22 14:51 UTC] cellog@php.net
this bug really shows how one person's bug is another person's 
feature. :)

the syntax $a->method() allows you to call a static method of the 
class that $a is an instantiated object of, something I find useful 
for objects that contain static methods in a parent/child 
inheritance hierarchy.  Using $a->method(), I don't need to do 
hackery to figure out which class "$a" is in order to call one of 
its static methods.  Consider the alternative:

<?php
call_user_func_array(get_class($a), 'method', $args);
?>

The above is the only alternative (save using reflection, which is 
even more verbose and inefficient) to:

<?php
$a->method($arg1, $arg1);
?>

Which syntax do you prefer?

The big difference between php 4 and php 5 is that a method declared 
as "static" does not have $this set.  You'll get a fatal error, in 
fact, if you try to use $this in a static method.

Why do you care so much about whether it's called with 
class::method() or $this->method()?  You can't have two methods with 
the same name, one static and one non-static, so there is no 
possibility of accidentally calling the wrong one.

If you are wanting absolutely "perfect" OO, there are plenty of 
other languages that will provide exactly the straightjacket and 
punishment you desire.  If you want to code efficient, easy to 
maintain, working programs, use PHP.
 [2007-03-22 16:38 UTC] andrea at 3site dot it
That's what I think about this feature.
http://webreflection.blogspot.com/2007/03/php-5-developers-teach-us-what-does.html

If a method is static, it should be static ... with PHP 5 a static method become an instance method (not static) ... what a feature!

The solution is to remove static keyword from my static methods, well done!

I hope PHP 6 will not have this ambiguity, regards.
 [2007-03-23 09:55 UTC] andrea at 3site dot it
Finally a developer show me what PHP 5 developers did with static behaviour ... it's totally the same of Java language but you probably forget a tiny thing: PHP is not Java and Java supports methods overload too!

At this point you implemented a keyword that get us less features than Java but its behaviour is the same of Java.

I remember you that PHP isn't "pseudo" compiled, isn't fast as Java should be and, as I've just said, it doesn't support "pure" method overload so with Java You can change a method behaviour even if one overload is static but you can't do the same thing with PHP.

This is an OO logic "problem" (if you don't want to call them *bug*) and to copy some Java concept inside a scripting language is not the way to have a better PHP, IMHO.

Regards, Andrea Giammarchi
 [2016-11-24 00:47 UTC] tctitans at yahoo dot com
I know this thread is old, but couldn't help myself but to add that there is no bug here and it is perfectly pure OO the way PHP handle static functions.

Quantifying a method as static indicates that it can be run independently of an instantiated object or not, and hence it ensures (error) that non-static member data cannot be referenced (no $this).  That is it.

This has nothing to do with how the function is called.  If you have an instantiated object and call (->) a static method, it processes the scope to find the method, and it does.   If you don't have an instantiated object, then you must explicitly define the class  Class::/self::

This is not a bug.  In fact, if it worked differently than this, then it would be a bug.
 [2016-11-24 00:54 UTC] tctitans at yahoo dot com
Don't look at the way Java handles static functions... Java doesn't do it properly (like so many other things).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 07:01:29 2024 UTC