php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33856 $this shining through
Submitted: 2005-07-25 23:46 UTC Modified: 2005-07-26 00:14 UTC
From: sr at brightlight dot ch Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.* OS: *
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: sr at brightlight dot ch
New email:
PHP Version: OS:

 

 [2005-07-25 23:46 UTC] sr at brightlight dot ch
Description:
------------
If a method is called via object::method(); from within a 
class method, then inside of the object::method(); the $this 
from the calling class method is visible.
This makes it almost impossible to tell if a method was 
invoked via -> or ::

Reproduce code:
---------------
<?php
	$test	= new testclass();
	$test->test();

	class testclass	{
		function test() {
			otherclass::staticFunction();
			normalFunction();
		}
	}
	
	class otherclass {
		function staticFunction() {
			echo "staticFunction: ".(isset($this) ? "set and of class: ".get_class($this)."\n" : "not set\n");
		}
	}

	function normalFunction() {
		echo "normalFunction: ".(isset($this) ? "set and of class: ".get_class($this)."\n" : "not set\n");
	}
?>

Expected result:
----------------
staticFunction: not set
normalFunction: not set

Actual result:
--------------
staticFunction: set and of class: testclass
normalFunction: not set

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-25 23:50 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

Mark the function as static. It is only possible to call non static methods as static methods for BC reasons.
 [2005-07-26 00:14 UTC] sr at brightlight dot ch
Well, maybe it is not intended that way anymore, but as long 
as calling a function that way does not result in an error-
message it should be done correctly. A function opens a new 
scope and within that scope, $this should not be visible (as 
any other variable from the calling scope).
It is clear to me that this problem does not appear for pure 
static functions, but I happen to have some hybrid 
functions, that's why I discovered this problem at all.
But a question besides this: if calling via :: is only 
possible with static functions in future - is there still a 
way for developing hybrid functions that can be called 
either via -> or :: or is the only possible solution to have 
two different functions for the same?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC