php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #12622 scope of $this and static functions
Submitted: 2001-08-07 11:00 UTC Modified: 2002-09-14 04:16 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: jh at synergy dot cx Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.0.6 OS: linux 2.2
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: jh at synergy dot cx
New email:
PHP Version: OS:

 

 [2001-08-07 11:00 UTC] jh at synergy dot cx
When calling a static function from within another
class the $this - reference is not empty, but it points
to the class in which you call the static function. To
illustrate:

class Static
{ function method()
  { echo get_class($this);
  }
}

class Container
{ function Container()
  { Static::method();
  }
}

$Container = new Container();

The output is "Container", but there shouldn't be a reference, because it's not anymore the scope of the
reference.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-21 20:10 UTC] sniper@php.net
Reproduced with PHP 4.1.0RC1.

Here is better example script (static is reserverd keyword):

<?php

class blaah
{
    function method()
    {
        echo get_class($this);
    }
}

class Container
{
    function Container()
    {
        blaah::method();
    }
}

$Container = new Container();

?>

 [2002-09-13 00:08 UTC] shamim at poetryunlimited dot com
Further examples - running PHP4.2.2 and NT4/Linux 2.4

Simple example that fails

<?php
error_reporting(E_ALL);
class A
{
  var $a;
  // This should be a static call - the only way it makes sense.
  function example(){echo $this->a;B::Example();}
}

class B extends A
{
  var $b;
  // This should fail when called from objects of type A.
  function example(){echo $this->b;}
}
$a=new A;
$a->example();
?>


Simple example that succeeds when it should not

<?php
error_reporting(E_ALL);
// 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();
?>
 [2002-09-14 04:16 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

This is how it is implemented, no bug.

Derick
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 27 16:01:27 2024 UTC