Solved

echo the name of each object and method I create

Posted on 2014-03-08
4
291 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
[X]
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
  • 2
  • 2
4 Comments
 
LVL 110

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 110

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 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