ホームページ >バックエンド開発 >PHPチュートリアル >オブジェクト指向の詳細な分析 php_PHP チュートリアル
これまで、オブジェクト指向の学習は暗記に限定されていましたが、これほど長い間働いてきた後、振り返ってみると、誰もが注目できる新しい洞察が得られました。
1.final
final: php5 は新しい Final キーワードを追加します。親クラスのメソッドがfinalと宣言されている場合、サブクラスはそのメソッドをオーバーライドできません。クラスがfinalと宣言されている場合、そのメソッドは継承できません。
public function setCity($city){$this->city = $city;}
public function getCity(){return $this->city;}
public function set Country($country){$this->gt; country = $country;}
public function get Country(){return $this->country;}
}
class Person{
protected $name;
protected $address;
//浅いクローン
public function __construct(){
address = new Address;
}
パブリック関数 __call($method,$arguments){
if(method_exists($this->address,$method)){
return call_user_func_array(array($this->address,$method),$arguments); ️ディープ クローン
$test1 = 新しい人;
$test2 = クローン $test1;
$test1->setCity('testcity1');
$test2->setName('testname2');
$test2->setCity('testcity2' );
echo $test1->getName().'-'.$test1->getCity()."n";
echo $test2->getName().'-'.$test2->getCity( )."n";
//testname1-testcity2
//testname2-testcity2
6. 重要な属性アクセス (__set __get __isset __unset) __isset __unset5.1 は
関数の後にのみ有効です: アクセスをインターセプトします属性の要件に合わせて、分離の度合いを改善するために、__isset() と __unset() も実装する必要があります。これにより、isset を使用して属性を検出したり、unset() を使用して属性を削除したりするときに、例:
コードをコピーします
コードは次のとおりです:
public function __get($property){
if(isset($this->__data[$property])){
return $this->__data[$property];
}else{
return false;
}
}
public function __set($property,$value){
if(isset($this->__data[$property])){
return $this->__data[$property] = $value;
}else{
return false;
}
}
public function __isset($property){
if(isset($this->__data[$property])){
return true;
}else{
return false;
}
}
public function __unset($property){
if(isset($this->__data[$property])){
return unset($this->__data[$property]);
}その他{
return false;
}
}
}
$test = 新しい人;
$test->email= 'test';
var_dump($test->email);