php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #52427 $this is used from the caller scope if the called method is static
Submitted: 2010-07-24 10:34 UTC Modified: 2013-02-18 00:34 UTC
From: tyra3l at gmail dot com Assigned:
Status: No Feedback Package: Class/Object related
PHP Version: 5.3.3 OS: all
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tyra3l at gmail dot com
New email:
PHP Version: OS:

 

 [2010-07-24 10:34 UTC] tyra3l at gmail dot com
Description:
------------
Samuel pointed out this "feature" on the php-windows mailing list:
http://marc.info/?t=127981502400008&r=1&w=2
this "feature" bit many people (see the bug reports, the comments on 
language.oop5.static.php), so I think we should add this behaviour to the 
documentation:

http://php.net/manual/en/language.oop5.static.php
"Because static methods are callable without an instance of the object
created, the pseudo-variable $this is not available inside the method
declared as static."

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

We should add that the non-static methods can be called via the scope resolution 
operator (aka the static way) but in this case the method will have access to 
the caller method's scope and an E_STRICT warning will be raised.

Test script:
---------------
<?php
class A{

 public $foo = 'bar';

 public function write(){

   print($this->foo);

 }

}


class B{

 public $foo = 'gnagnagna';



 public function write(){

  A::write();

 }

}


$var = new B;

$var->write();

Expected result:
----------------
PHP Fatal error:  Using $this when not in object context in...
OR
Change in the documentation to describe this kind of behaviour.

Actual result:
--------------
gnagnagna

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-24 11:28 UTC] tyra3l at gmail dot com
http://www.php.net/manual/en/language.oop5.basic.php
"The pseudo-variable $this is available when a method is called from within an 
object context. $this is a reference to the calling object (usually the object to 
which the method belongs, but possibly another object, if the method is called 
statically from the context of a secondary object)."

So it is indeed documented, maybe not on the best place of all...
 [2010-08-15 09:43 UTC] degeberg@php.net
-Status: Open +Status: Feedback
 [2010-08-15 09:43 UTC] degeberg@php.net
Where do you propose it should be documented instead?
 [2010-08-15 10:18 UTC] tyra3l at gmail dot com
http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php
http://php.net/manual/en/language.oop5.static.php

this was the first two place where I tried to find documentation about this 
behavior.

Tyrael
 [2013-02-18 00:34 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 23 03:01:28 2024 UTC