Pavel Parvez
Pavel Parvez

Reputation: 13

Print class properties using custom function

Its not working. print only name. how can i print all properties? i cant find my error. please someone solve it with description. my code is...

 <?php
   class pavel{
     public $name="pavel"; 
     public $age="23";
     public $degree="CSE";
      public function myself_set($a,$b,$c){
        $this->name=$a;
        $this->age=$b;
        $this->degree=$c;
      }

      public function myself_get(){
        return  $this->name."<br />";
        return  $this->age."<br />";
        return  $this->degree."<br />";
      }


   }
   $obj = new pavel();
   $obj->myself_set("parvej","24","BSc");
   echo $obj->myself_get();
   echo $obj->myself_get(); 
   ?>

Upvotes: 1

Views: 33

Answers (1)

trincot
trincot

Reputation: 350272

In this function:

public function myself_get(){
    return  $this->name."<br />";
    return  $this->age."<br />";
    return  $this->degree."<br />";
}

... only the first return statement is executed, at which point the function exits back to the caller. So the other return statements are "dead code": they will never be executed.

Change to something like:

public function myself_get(){
    return  $this->name."<br />".
            $this->age."<br />".
            $this->degree."<br />";
}

This will return one string made up of the three values.

See it run on eval.in

Upvotes: 1

Related Questions