Solved

echo the name of each object and method I create

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

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP extract HTML using DOMXpath from nested tables 8 47
calculated column 12 74
Password forgotten. 3 40
incrementing radio button names and ids using php 5 37
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

929 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now