Sia nel primo libro che in questo secondo che sto leggemdo non viene detto nulla in merito all'uso del dollaro quasi come fosse una questione sottintesa. Nel primo libro si dice solo che $ sta per modificabile ma a me risulta che se una variabile non é definita come final può essere comunque ancora modificata.
Qui il metodo che mi ha fatto riflettere:
Codice PHP:
<?php
class Person {
private $_name;
private $_age;
function __set( $property, $value ) {
$method = "set{$property}";
if ( method_exists( $this, $method ) ) {
return $this->$method( $value );
}
}
function setName( $name ) {
$this->_name = $name;
if ( ! is_null( $name ) ) {
$this->_name = strtoupper($this->_name);
}
}
function setAge( $age ) {
$this->_age = strtoupper($age);
}
function stampanome(){
print $this->_name;
}
}
$p = new Person();
$p->name = "bob";
$p->stampanome();
Per esempio se scrivo:
Codice PHP:
function setName( $name ) {
$this->$_name = $name;
if ( ! is_null( $name ) ) {
$this->$_name = strtoupper($this->$_name);
}
}
o anche:
Codice PHP:
$p->$name = "bob";
ottengo:
Notice: Undefined variable: name in C:\xampp\htdocs\index.php on line 25