Mega Code Archive

 
Categories / Php / Class
 

Pass class instance as parameter

<?php     class Employee {         public $title;         public $lastName;         public $firstName;         public $price;                  function __construct( $title, $firstName, $mainName, $price ) {              $this->title     = $title;             $this->firstName = $firstName;             $this->lastName  = $mainName;             $this->price     = $price;         }         function getFullName() {             return "{$this->firstName}" . " {$this->lastName}";         }     }      class EmployeeWriter {     public function write( $shopProduct ) {         $str  = "{$shopProduct->title}: ";            $str .= $shopProduct->getFullName();         $str .= " ({$shopProduct->price})\n";         print $str;     } } $product1 = new Employee( "Title", "A", "B", 5.9 ); $writer = new EmployeeWriter(); $writer->write( $product1 ); ?>