php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28145 $this / instanceof issue with static calls of instance functions
Submitted: 2004-04-25 17:26 UTC Modified: 2004-04-26 11:25 UTC
From: wf at bitplan dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5* OS: *
Private report: No CVE-ID: None
 [2004-04-25 17:26 UTC] wf at bitplan dot com
Description:
------------
I am using PHP5 RC2 (couldn't yet select it - have downloaded it a few minutes ago ...) on Windows XP Professional with Apache2.0.49. 
This bug is similar to #20089 but I'm complaining more about the instanceof issue. The true fix is of course fixing #20089 first. Saying "This is not a bug" is not a good thing with the goal of better object orientation for PHP5 ...

When calling an instance function ("->" needed) as a class function ("::") this should either be an error or instanceof for the given class should fail - it looks like 
a) instanceof returns "" instead of FALSE/0 when 
this is not the correct instance
b) when calling an instance function of another class in a static way this should be an error 

Reproduce code:
---------------
<?php
class Foo
{
  function __construct  () { echo 'Generating a Foo object <br />'; }    
  function getInstance  () { return new Foo();	                    }	    
  function operation    () { echo "Foo operation this is a ".get_Class($this)."<br>";
                             echo "This is a Foo: ".$this instanceof Foo."<br>"; }	
  function baroperation () { Bar::operation();                      }
}

class Bar 
{
  function __construct  () { echo 'Generating a Bar object <br />'; }    
  function getInstance  () { return new Bar();	                    }	    
  function operation    () { echo "Bar operation this is a ".get_Class($this)."<br>";
                             echo "This is a Bar: ".$this instanceof Bar."<br>"; }	
  function fooOperation () { Foo::operation();                      }
}
$foo = Foo::getInstance();
$bar = Bar::getInstance();
$foo->operation();
$bar->operation();
$foo->baroperation();
$bar->foooperation();
Foo::operation();
Bar::operation();
?> 

Expected result:
----------------
Generating a Foo object
Generating a Bar object
Foo operation this is a Foo
This is a Foo: 1
Bar operation this is a Bar
This is a Bar: 1
error Bar operation but this is a Foo
This is a Bar: 0 
error Foo operation but this is a Bar
This is a Foo: 0
Foo operation this is a nil
This is a Foo: 0
Bar operation this is a nil
This is a Bar: 0


Actual result:
--------------
Generating a Foo object
Generating a Bar object
Foo operation this is a Foo
This is a Foo: 1
Bar operation this is a Bar
This is a Bar: 1
Bar operation this is a Foo
This is a Bar: 
Foo operation this is a Bar
This is a Foo:
Foo operation this is a
This is a Foo:
Bar operation this is a
This is a Bar: 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-25 18:11 UTC] helly@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

In a statically called method $this is NULL and NULL representation is the empty string.

Regarding #20089: In a stattically called method there cannot be a $this because it is a local variable generated by the engine. the behavior of PHP 4 is wrong and is there only because there simply was no way to implement it correct. One of the reasons it could not be done correct is the lack of real static methods.
 [2004-04-26 11:25 UTC] wf at bitplan dot com
thanks for the feedback. 
$this=null sounds ok for me
instanceof null should be false though and not null ... that was the main point of my bug report
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC