php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44282 new function get_object_name()
Submitted: 2008-02-28 10:55 UTC Modified: 2013-03-22 15:37 UTC
Votes:8
Avg. Score:3.8 ± 1.3
Reproduced:6 of 7 (85.7%)
Same Version:5 (83.3%)
Same OS:3 (50.0%)
From: msaspence at gmail dot com Assigned: reeze (profile)
Status: Closed Package: *General Issues
PHP Version: 5.2.5 OS:
Private report: No CVE-ID: None
 [2008-02-28 10:55 UTC] msaspence at gmail dot com
Description:
------------
Currently to get the objects variable name within the object you must pass it as a parameter and have the constructor define it.

This solution while simple would be tidier if there was a function that could retrieve the object's variable instance from inside itself.


Reproduce code:
---------------
Currently

<?php

class myClass {
    
    function __construct($objectName) {
        $this->objectName = $objectName;
    }
    
    function printName() {
        echo "the name of this object instance is ".$this->objectName;
    }

}

$object = 'myObject';
$$object = new myClass('$object');
$$object->printName();

?> 

Expected result:
----------------
How it might be done:
<?php

class myClass {
    
    function printName() {
        echo "the name of this object instance is ".$this->get_object_name();
    }

}

$object = 'myObject';
$$object = new myClass;
$$object->printName();

?> 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-22 15:37 UTC] reeze@php.net
I thought __toString() magic method is what you want. close for now
 [2013-03-22 15:37 UTC] reeze@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: reeze
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Aug 18 17:01:27 2019 UTC