Solved

echo the name of each object and method I create

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

708 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

17 Experts available now in Live!

Get 1:1 Help Now