Solved

echo the name of each object and method I create

Posted on 2014-03-08
4
288 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

773 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