php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30355 Weird Behaviour calling non-static method statically
Submitted: 2004-10-07 22:51 UTC Modified: 2004-10-08 21:14 UTC
Votes:4
Avg. Score:4.2 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:3 (75.0%)
From: info at rhalff dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.0.2 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
19 - 14 = ?
Subscribe to this entry?

 
 [2004-10-07 22:51 UTC] info at rhalff dot com
Description:
------------
I think the behaviour below is allready known and it can be fixed declaring the method 'static', however I still think this is a bug. The script should fail instead of continuing incorrect behaviour.

Related to Bug #9005, #12622, #20089, #25220, #29206

Because $this behaviour was allready there in 2001, I thought $this would be a good reminder $this feature is still present in 5.0.2


Reproduce code:
---------------
<?php

class A {

    public function test()
    {
        $this->setValue('huh?');
    }
    
    public function test2()
    {   
        $this->nonExistent();
    }

}   

class B {
    
    public function test()
    {   
        A::test();
    }
    public function setValue($v)
    {   
        echo "$v";
    }
    
    public function test2()
    {   
        A::test2();
    }



}

$B = new B;
$B->test();
$B->test2();


?>

Expected result:
----------------
script should fail, cannot call a non-static method statically.




Actual result:
--------------
huh?
Fatal error: Call to undefined method B::nonExistent() in /var/www/hosts/gedicht.nu/docs/static/test.php on line 12

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-08 08:55 UTC] derick@php.net
Marcus, can you explain why there is no bug here?
 [2004-10-08 21:14 UTC] helly@php.net
For BC reasons calling a non static method statically keeps $this. 

This is not my decision - often enough i suggested to fix static member behavior now that we can declare them in PHP 5. If you ask me there is no OO in PHP 4 and hence we should never have taken care of BC with 4.
 [2011-02-03 14:51 UTC] carlos dot vini at gmail dot com
This bug can be exploited for creating a kind of "multiple inheritance":

<?php

class Airplane {
	public function getName() {
		return 'Airplane ' . $this->name;	
    }
	public function getPosition() {
		return '- Air.';
	}
}

class Car {
	public function getName() {
		return 'Car ' . $this->name;	
	}
	public function getPosition() {
		return '- Ground.';
	}
}

class FlyingCar extends Car {
	public function getName() {
		return Airplane::getName();
	}
}

$a = new FlyingCar();
$a->name = 'Example';
echo $a->getName();
echo $a->getPosition();
?>

Expected result:
------------------------
$this does not exist in a static context


Actual result:
-----------------------
It echoes: Airplane Example - Ground.
 [2012-07-25 08:46 UTC] baptiste33 at gmail dot com
A few years later.. It's it planned to keep this "bug" as the documentation still 
says :

"Calling non-static methods statically generates an E_STRICT level warning."

Which is obviously not true.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 07:01:29 2024 UTC