Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

echo the name of each object and method I create

Posted on 2014-03-08
4
289 Views
Last Modified: 2014-03-09
// Define the class:
class Demo {

    // No attributes.
    
    // Constructor:
    function __construct() {
        echo '<p>In the constructor.</p>';
    }

    // Destructor:
    function __destruct() {
        echo '<p>In the destructor.</p>';
    }
    
} // End of Demo class.

Open in new window



I want to echo the name of each object and method I create

obj1 is in the destructor

obj3 is in the constructor
0
Comment
Question by:rgb192
  • 2
  • 2
4 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39915791
Let's try to get the terminology correct.  

A class definition is a piece of PHP code.  The code snippet above contains the Demo class.  You created the class when you wrote the PHP code.

A method is a piece of PHP code that is encapsulated inside the class definition.  It is a function that is localized to the class.  If I have Class A and Class B, they can both contain identically named methods, and there is no conflict in naming because of the encapslation.

An object is the in-memory representation of the class.  When a class name is referenced in an assignment statement that uses the new keyword, an object instance of the class is created.  For example, in this assignment statement, the $d variable contains an object that is an instance of the Demo class.

$d = new Demo;

There are some functions and magic constants associated with PHP classes and methods.  Please see the man page references and examples here.  But beware of old comments in the user-contributed notes, since they may refer to the PHP4 object model which was pretty lame and is mostly outdated today.

http://php.net/manual/en/ref.classobj.php especially:
http://php.net/manual/en/function.get-class.php

http://php.net/manual/en/language.constants.predefined.php

See http://www.iconoun.com/demo/temp_rgb192.php for an example of using the magic constants.

<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28383735.html

Class Thing
{
    public function __construct()
    {
        $this->msg = 'Hello World';
    }

    public function speak()
    {
        echo 'Class: ' . '<b>' . __CLASS__  . '</b>'
        . ', Method: ' . '<b>' . __METHOD__ . '</b>'
        . ' says: '    . '<b>' . $this->msg . '</b>'
        . PHP_EOL
        ;
    }
}

$t = new Thing;
$t->speak();

Open in new window

0
 

Author Comment

by:rgb192
ID: 39915985
In your code example, I am looking to echo object name $t


object is named: $t
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39916118
Uhh, why?  You already know the name of the variable you used to instantiate the Thing class.  Because you wrote it!

Maybe you can modify this function.
<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28383735.html

Class Thing
{
    public function __construct()
    {
        $this->msg = 'Hello World';
    }

    public function speak()
    {
        echo 'Class: ' . '<b>' . __CLASS__  . '</b>'
        . ', Method: ' . '<b>' . __METHOD__ . '</b>'
        . ' says: '    . '<b>' . $this->msg . '</b>'
        . PHP_EOL
        ;
    }
}

$t = new Thing;
$t->speak();

echo '<br>' . PHP_EOL;
dump_r($t);


// FUNCTION TO DISPLAY THE VARIABLE NAME AND VALUE
function dump_r(&$var, $return=FALSE)
{
    // THE VARIABLE NAME THING WE ARE LOOKING FOR
    $vname = '?';

    // GET A COPY OF ALL OF THE PHP VARIABLES
    $vals = $GLOBALS;

    // SINCE WE PASS THE VAR BY REFERENCE, WE MUST SAVE A COPY OF THE OLD VALUE
    $old  = $var;

    // MUNG THE VALUE OF OUR VARIABLE TO A UNIQUE VALUE
    $var  = $new = 'UNIQ' . $_SERVER["UNIQUE_ID"] .'VALU';

    // FIND THE MUNGED VALUE IN A SHALLOW COPY OF OUR VARIABLES (NO EXPLORATION OF ARRAYS OR OBJECTS)
    foreach($vals as $key => $val)
    {
        // LOOKING FOR ARRAY - NO RECURSION, ONE ROW DEEP ONLY
        if (is_array($val))
        {
            foreach ($val as $akey => $aval)
            {
                if ($aval === $new) $vname = $key . '[' . $akey . ']';
            }
        }

        // LOOKING FOR OBJECT - NO RECURSION, ONE ROW DEEP ONLY
        if (is_object($val))
        {
            foreach ($val as $okey => $oval)
            {
                if ($oval === $new) $vname = $key . '=>' . $okey;
            }
        }

        // LOOKING FOR SCALAR
        if ($val === $new) $vname = $key;
    }

    // RESTORE THE VAR TO ITS OLD VALUE
    $var = $old;

    // GET THE RESULTS
    ob_start();
    echo "<b>\${$vname}</b> === ";
    var_dump($var);
    $str = rtrim(ob_get_contents()) . PHP_EOL;
    ob_end_clean();

    // RETURN OR DISPLAY THE RESULTS?
    if ($return)
    {
        return $str;
    }
    else
    {
        echo $str;
    }
}

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 39916545
works. I ran with debugger and tried to understand. You told me there was no use to echo object name so I will send you questions about the larry ulman tutorial.

thank you
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question