Modelde değişkenler ayarlarken $ bu return'ing amacı nedir

2 Cevap php

Ben kendi kendine düşünce hobi programcı değilim ve bu nedenle uzmanları yapmak şekilde aşağı daima temellerini yok. Bu temel Yani eğer beni lütfen bağışlayın.

What is the purpose or benefit of return'ing $this when setting vars in the model. I have seen this done in other places too but cant figure this out.

Örnek kod:

public function setAlias($Alias){
    $this->_Alias = (string) $Alias;
    return $this;
}

public function getAlias(){
    return $this->_Alias;
}

2 Cevap

Bu yöntem zincirleme yapmanızı sağlar. Örneğin,

$object = new Object();
$object->setAttribute1("value")->setAttribute2("value")

Bu yöntem zincirleme sağlar:

$someObj->method1()->method2();