php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9005 Static classes sometimes have a $this pointer
Submitted: 2001-01-30 13:39 UTC Modified: 2001-03-08 05:51 UTC
From: chuck at horde dot org Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.4pl1 OS: Linux 2.2
Private report: No CVE-ID: None
 [2001-01-30 13:39 UTC] chuck at horde dot org
If you call a static method from within an object, the static method will inherit the $this pointer of the calling object. This script demonstrates the problem:

<?php

error_reporting(E_ALL);

class StaticClass {
    function doSomething() {
        echo $this->data;
    }
}

class Instantiated {
    var $data;

    function Instantiated($data) {
        $this->data = $data;
        StaticClass::doSomething();
    }
}

$ob = new Instantiated('foo');

?>


This is rather confusing behavior - $this should be out of scope inside static methods no matter what.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-08 05:51 UTC] zeev@php.net
class::method() isn't a static call in PHP (it isn't always a static call in C++ either).
The primary use for class::method() is actually calling methods from your parent class, not calling 'static methods' (the ability to create static classes and call their methods is pretty much a positive 'side effect').

So, this is the intended behavior.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 24 00:01:27 2024 UTC