how can I echo name using __get magic method

<?php
class person{
  public $name;
  
  public function __construct($name,$age){
    $this->name=$name;
    $this->age=$age;
  }
  public function __get1($variable){
    echo '<h1>this is the variable: '.$variable.'</h1>';
  }
}

$obj=new person('jon',29);
echo '<br>name is: '.$obj->__get1($obj->name);

Open in new window



I do not think I am using magic_method properly because I switch __get to __get1 and code runs the same

how can I echo name using __get magic method
LVL 1
rgb192Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ray PaseurConnect With a Mentor Commented:
Please see: http://www.iconoun.com/demo/temp_rgb192.php

Note that there is a magic method __get() defined in the Person class.  The magic method is called "magic" because it is invoked without being explicitly called in your code.  The constructor __construct() is also a magic method, just like __toString() and __Destruct, etc.

In PHP the definition of overloading is different from most languages.  Don't ask me why.

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

class Person
{
    protected $name;

    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age  = $age;
    }

    public function __get($var)
    {
        if (isset($this->$var))
        {
            echo '<h1>MAGIC METHOD __GET() CALLED FOR ' . "<i>$var</i>" . ': ' . $this->$var . '</h1>';
        }
        else
        {
            echo '<h1>MAGIC METHOD __GET() CALLED FOR UNDEFINED: ' . "<i>$var</i>" . '</h1>';
        }
    }
}

// CREATE AN INSTANCE OF THE PERSON CLASS
$obj = new person('jon',29);

// SHOW THE OBJECT INSTANCE OF THE PERSON CLASS
var_dump($obj);

// MUTATE THE AGE AND SHOW THE CLASS AGAIN
$obj->age = -14;
var_dump($obj);

// TRY TO GET THE $name PROPERTY
echo $obj->name;

// TRY TO GET AN UNDEFINED PROPERTY
echo $obj->pludgort;

Open in new window

Now after you have run that script, remove the __get() magic method as shown below and run it again.  You will get a fatal error.  The __get() method is there to give you a way to prevent a fatal run-time error when you (or more likely another programmer) makes a mistake.

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

class Person
{
    protected $name;

    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age  = $age;
    }

/*
    public function __get($var)
    {
        if (isset($this->$var))
        {
            echo '<h1>MAGIC METHOD __GET() CALLED FOR ' . "<i>$var</i>" . ': ' . $this->$var . '</h1>';
        }
        else
        {
            echo '<h1>MAGIC METHOD __GET() CALLED FOR UNDEFINED: ' . "<i>$var</i>" . '</h1>';
        }
    }
*/
}

// CREATE AN INSTANCE OF THE PERSON CLASS
$obj = new person('jon',29);

// SHOW THE OBJECT INSTANCE OF THE PERSON CLASS
var_dump($obj);

// MUTATE THE AGE AND SHOW THE CLASS AGAIN
$obj->age = -14;
var_dump($obj);

// TRY TO GET THE $name PROPERTY
echo $obj->name;

// TRY TO GET AN UNDEFINED PROPERTY
echo $obj->pludgort;

Open in new window

0
 
Marco GasiFreelancerCommented:
__get() magic method is used to get data from Inacessible property, that is, for instance, from a private property. Your $name is public, so it can be simply accessed with any method you use, giving it whatever name.

Set the property to private: __get1 will fail and __get will success.
0
 
Ray PaseurCommented:
Man Page Ref Here (always look this stuff up!):
http://www.php.net/manual/en/language.oop5.overloading.php#object.get
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Ray PaseurCommented:
Let's start with this.  It creates some output but it does not make much sense.
http://www.iconoun.com/demo/temp_rgb192.php

Line 6 declares the $name property to be public.  That is the default, so this line has no programmatic meaning.  It does no harm nor any good.

Conspicuous by its absence is any declaration for the $age property.  Without a visibility declaration, $age (and any other undeclared properties) is a public property of the object.

Line 8-12 creates the constructor that assigns the external values in the function definition statement, $name and $age, to object properties.

Line 14-17 creates the myGet() method.  The method takes an external variable named $variable and uses it in an echo statement.  It makes no reference to any of the object properties, working only with the variable in the function call.

Why did I change the name of __get1() to MyGet()?  Because PHP has reserved all function names starting with two underscores for internal PHP use.  You can't name your function that way if you want to write dependable PHP code.

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

class person
{
    public $name;
  
    public function __construct($name,$age)
    {
        $this->name=$name;
        $this->age=$age;
    }

    public function myGet($variable)
    {
        echo '<h1>this is the variable: '.$variable.'</h1>';
    }
}

$obj=new person('jon',29);

// SHOW THE OBJECT
var_dump($obj);

Open in new window

In a little while I'll try to show you how to use a getter function.
0
 
rgb192Author Commented:
<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

class person
{
    public $name;
  
    public function __construct($name,$age)
    {
        $this->name=$name;
        $this->age=$age;
    }

    public function myGet($variable)
    {
        echo '<h1>this is the variable: '.$variable.'</h1>';
    }
}

$obj=new person('jon',29);

// SHOW THE OBJECT
var_dump($obj);
echo 'I add this line to see if my ide has entered myGet method';

Open in new window

ide has not entered myGet method


okay __get() will work with private and ide will not enter __get1()
<?php
class person{
  private $name;
  
  public function __construct($name,$age){
    $this->name=$name;
    $this->age=$age;
  }
  public function __get1($variable){
    echo '<h1>this is the variable: '.$variable.'</h1>';
  }
}

$obj=new person('jon',29);
echo '<br>name is: '.$obj->__get1($obj->name);

Open in new window



Fatal error: Cannot access private property person::$name in C:\Users\Acer\Documents\NuSphere PhpED\Projects\noname309.php on line 15



I think I am not understanding magic method
for example
__tostring is 'magically used' when attempting to echo an object
__destruct is 'magically used' at the end of code

this is not magic:
$obj->__get($obj->name);
I am telling the $obj to enter its get method using parameter $obj->name
0
 
Ray PaseurCommented:
this is not magic:
$obj->__get($obj->name);
I am telling the $obj to enter its get method using parameter $obj->name
I think it's not written out that way.  Give me a moment to try to put an example together using the earlier script.
ide has not entered myGet method
Of course not -- it was never called in place in the code.
0
 
Marco GasiConnect With a Mentor FreelancerCommented:
rgb192, in my comment ID 40031219 I gave my opinion.

If you try what I said, you see it works. In you comment ID 40031876 you post two snippets: in the former you set $name variable as public, in the latter the variable is private.

__get allows to acces inaccessible properties (http://www.php.net/manual/en/language.oop5.magic.php) so, using your second snippet this way:

<?php
class person{
  private $name;
  
  public function __construct($name,$age){
    $this->name=$name;
    $this->age=$age;
  }
  public function __get($variable){
    echo '<h1>this is the variable: '.$variable.'</h1>';
  }
}

$obj=new person('jon',29);
echo '<br>name is: '.$obj->__get($obj->name);

Open in new window


Setting $name variable as private and using __get (not __get1) gives this output:

Output of script
The magic just resides in the fact you can access a private variable: in fact if you use $obj->get($obj->name) instead of $obj->__get($obj->name) you get the Fatal error:
Fatal error: Call to undefined method person::__get() in C:\xampp\htdocs\empty.php on line 15
0
 
rgb192Author Commented:
thanks

The magic just resides in the fact you can access a private variable


this appeared more 'magical' because I did not see the word 'get'
// TRY TO GET THE $name PROPERTY
echo $obj->name;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.