class MyClass {
protected $ data = array ( 'some' => array ( 'sub' => 'data' ) ) ;
public function __set ( $ name , $ value ) {
$ this -> data [ $ name ] = $ value ;
}
public function __get ( $ name ) {
return $ this -> data [ $ name ] ;
}
}
$ my = new MyClass ( ) ;
$ my -> some [ 'sub' ] = 'test' ; // trying to change the value
echo $ my -> some [ 'sub' ] ; // displays 'data'
Notice: Indirect modification of overloaded property MyClass :: $ some has no effect
class ActiveArray {
protected $ array ;
public function __construct ( $ array ) {
$ this -> array = & $ array ;
}
public function __set ( $ name , $ value ) {
$ this -> array [ $ name ] = $ value ;
}
public function __get ( $ name ) {
if ( is_array ( $ this -> array [ $ name ] ) )
return new self ( & $ this -> array [ $ name ] ) ;
else
return $ this -> array [ $ name ] ;
}
}
public function __get ( $ name ) {
if ( is_array ( $ this -> data [ $ name ] ) )
return new ActiveArray ( & $ this -> data [ $ name ] ) ;
else
return $ this -> data [ $ name ] ;
}
$ my -> some -> sub = 'test' ;
echo $ my -> some -> sub ; // displays 'test'
$ my -> some = array ( 'abc' => 123 ) ;
echo $ my -> some -> abc ; // will print '123'
Source: https://habr.com/ru/post/103834/
All Articles