Solved

__get to echo name

Posted on 2014-04-29
2
277 Views
Last Modified: 2014-04-29
class person{
  
  public function __construct($name,$age){
  $this->name=$name;
  $this->age=$age;  
  }
  public function __get($variable){
    echo '<h1>this it the variable: '.$variable.'</h1>';
  }
}


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

Open in new window





I remember that the magic method __get is called but I forget how to call in order to echo name
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 500 total points
ID: 40031114
This should do it:

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


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

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 40031157
thanks
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

630 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