Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call multiple methods on object?

Tags:

php

I tried calling multiple functions on a single object. But I think I failed the syntax. Could you correct me please?

    $objMetaDaten->setStrTitle('test')
        ->setStrBeschreibung('test')
        ->setStrUeberschrift('test')
        ->setStrCanonical('test')
        ->setStrRobots(MetaDaten::INDEX);
like image 379
ElDiabolo Avatar asked Oct 23 '25 04:10

ElDiabolo


1 Answers

What you need is something called fluent setters which will return the object after calling a setter on the object as against the conventional void setters something like below

Class A{
   private $name;
   private $id;

   public function setName($name) {
        $this–>name = $name;
         return $this;
    } 

    public function getName() {
     return $this–>name;
     } 

     public function setId($id) {
        $this–>id = $id;
         return $this;
     } 

     public function getId() {
         return $this–>id;
     } 
} 

So you can then say

 $test = new A();

 $test->setId(1)->setName('Fredrick');
like image 117
Ghost Worker Avatar answered Oct 25 '25 17:10

Ghost Worker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!