Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

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
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

__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.
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.
Avatar of rgb192

ASKER

<?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
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.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

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;