rgb192
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);
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
Man Page Ref Here (always look this stuff up!):
http://www.php.net/manual/en/language.oop5.overloading.php#object.get
http://www.php.net/manual/en/language.oop5.overloading.php#object.get
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.
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);
In a little while I'll try to show you how to use a getter function.
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';
ide has not entered myGet methodokay __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);
Fatal error: Cannot access private property person::$name in C:\Users\Acer\Documents\Nu
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:I think it's not written out that way. Give me a moment to try to put an example together using the earlier script.
$obj->__get($obj->name);
I am telling the $obj to enter its get method using parameter $obj->name
ide has not entered myGet methodOf course not -- it was never called in place in the code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
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;
Set the property to private: __get1 will fail and __get will success.