I saw an example in a book recently that I don't fully understand and maybe someone can help clarify for me. The code in question is:
<?php
class Person {
function __get($property){
$method = "get{$property}";
if (method_exists($this, $method)){
return $this->$method();
}
}
function getName(){
return "bob";
}
}
$p = new Person();
echo $p->name;
?>
Which mostly makes sense to me, my only issue is this line:
$method = "get{$property}";
what are the brackets for and what do they do in this situation, I have never come across this. I am still learning, so this may be an easier question than I think. Thanks for you help!The braces are to ensure that $property is evaluated correctly to create the string used to call the function.
Seems like a somewhat useless example when you could just implement __call() to produce the same effect without the string wrangling <!-- m --><a class="postlink" href="http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing">http://www.php.net/manual/en/language.t ... ng.parsing</a><!-- m -->
<?php
class Person {
function __get($property){
$method = "get{$property}";
if (method_exists($this, $method)){
return $this->$method();
}
}
function getName(){
return "bob";
}
}
$p = new Person();
echo $p->name;
?>
Which mostly makes sense to me, my only issue is this line:
$method = "get{$property}";
what are the brackets for and what do they do in this situation, I have never come across this. I am still learning, so this may be an easier question than I think. Thanks for you help!The braces are to ensure that $property is evaluated correctly to create the string used to call the function.
Seems like a somewhat useless example when you could just implement __call() to produce the same effect without the string wrangling <!-- m --><a class="postlink" href="http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing">http://www.php.net/manual/en/language.t ... ng.parsing</a><!-- m -->