php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19384 Class methods passing $this context incorrectly when accessing static methods
Submitted: 2002-09-12 23:03 UTC Modified: 2002-09-14 10:49 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: shamim at poetryunlimited dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.2 OS: Linux/Windows NT4
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: shamim at poetryunlimited dot com
New email:
PHP Version: OS:

 

 [2002-09-12 23:03 UTC] shamim at poetryunlimited dot com
<?php
// Example of bug in PHP class method passing $this incorrectly

class test1
{
  var $a;
  function test1(){$this->a=1;}
  function showMe()
  {
    // Since test3::showMe was called as a static method
    // This too should be a static method call with no $this
    echo 'In test1::showMe<hr>';
    echo 'Next 2 lines should fail since this method was not called from within this object<br>';
    echo '$this is of type '. get_class($this)." in test3::showMe<br>\n";
    echo "test1::showMe:a=".$this->a."<br>\n";
  }
  // Class member to test static method call
  function callMe()
  {
    echo 'In test1::callMe<hr>';
    echo '$this is of type '.get_class($this)."in test1::callMe<hr>\n";
    echo 'Calling class method test3::showMe from an object of type test1<br>';
    echo '$this should not be passed since test3::showMe does not exist in objects of type test1<hr>';
    // This is a static method call, since test1 objects
    // do not have a test3::showMe.
    test3::showMe();
  }
}

class test2 extends test1
{
  function showMe()
  {
    echo 'In test2::showMe<br>';
    echo "Next 2 lines should fail since objects of type test1 cannot pass \$this<hr>\n";
    echo '$this is of type '. get_class($this)." in test2::showMe<br>\n";
    echo "test2::showMe:a=".$this->a."<hr>\n";
    echo 'Calling parent::showMe<hr>';
    // Since test3::showMe was called as a static method
    // This too should be a static method call with no $this
    parent::showMe();
  }
}

class test3 extends test2
{
  function showMe()
  {
    echo 'In test3::showMe<br>';
    echo "Next 2 lines should fail since objects of type test1 cannot pass \$this<hr>\n";
    echo '$this is of type '. get_class($this)." in test3::showMe<br>\n";
    echo "test3::showMe:a=".$this->a."<hr>\n";
    echo 'Calling parent::showMe<hr>';
    // Since test3::showMe was called as a static method
    // This too should be a static method call with no $this
    parent::showMe();
  }
}

// object $a is of type test1
$a=new test1;
$a->callMe();

/* Results

In test1::callMe
--------------------------------------------------------------------------------
$this is of type test1in test1::callMe
--------------------------------------------------------------------------------
 Calling class method test3::showMe from an object of type test1
$this should not be passed since test3::showMe does not exist in objects of type test1
--------------------------------------------------------------------------------
In test3::showMe
Next 2 lines should fail since objects of type test1 cannot pass $this
--------------------------------------------------------------------------------
 $this is of type test1 in test3::showMe
test3::showMe:a=1
--------------------------------------------------------------------------------
 Calling parent::showMe
--------------------------------------------------------------------------------
In test2::showMe
Next 2 lines should fail since objects of type test1 cannot pass $this
--------------------------------------------------------------------------------
 $this is of type test1 in test2::showMe
test2::showMe:a=1
--------------------------------------------------------------------------------
 Calling parent::showMe
--------------------------------------------------------------------------------
In test1::showMe
--------------------------------------------------------------------------------
Next 2 lines should fail since this method was not called from within this object
$this is of type test1 in test3::showMe
test1::showMe:a=1


*/
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Apr 08 17:01:31 2025 UTC