php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64886 Different return value between within or without an object
Submitted: 2013-05-21 02:38 UTC Modified: 2013-05-21 06:58 UTC
From: fm dot lheureux at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: Irrelevant OS: Ubuntu 13.04x64
Private report: No CVE-ID: None
 [2013-05-21 02:38 UTC] fm dot lheureux at gmail dot com
Description:
------------
I would expect a getter to return the same value whether it's being called within 
or without an object.

In my example, my __get function always returns null. If it's being called from 
outside the object, it behaves properly and returns null. If it's being called 
from inside, it returns the object itself.

Here I used the __get function to make it even clearer, but it does the same 
regardless of the presence of that function.

Test script:
---------------
<?php

class Toto{
    function __get($p){
        return null;
    }   

    function doit(){
        echo get_class($this->wololo); //this will print "TOTO", but why?
    }   
}

$t = new Toto();
$t->doit();
echo "\n";
echo get_class($t->wololo); //this will not work, which is what I expect
echo "\n";

Expected result:
----------------
PHP Warning:  get_class() called without object from outside a class in 
/home/fmlheureux/test.php on line 9
PHP Warning:  get_class() called without object from outside a class in 
/home/fmlheureux/test.php on line 16

Actual result:
--------------
Toto
PHP Warning:  get_class() called without object from outside a class in 
/home/fmlheureux/test.php on line 16

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-21 02:53 UTC] fm dot lheureux at gmail dot com
This bug can be killed. When writing it I realized my mistake. When the get_class 
method is invoked with the null parameter, it returns the class of where it's 
being called, hence "Toto".

Not a bug!
 [2013-05-21 06:58 UTC] nikic@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 [2013-05-21 06:58 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 17:01:31 2024 UTC