Switch to full style
:read: Start PHP with us. Includes topics to help you in php
Post a reply

class with constructor parameter

Sun Oct 26, 2008 11:49 pm

Code:
<?php
class PersonWriter {

    function
writeName( Person $p ) {
        print
$p->getName()."\n";
    }

    function
writeAge( Person $p ) {
        print
$p->getAge()."\n";
    }
}

class
Person {
    
private $writer;

    function
__construct( PersonWriter $writer ) {
        
$this->writer = $writer;
    }

    function
__call( $method, $args ) {
        if (
method_exists( $this->writer, $method ) ) {
            return
$this->writer->$method( $this );
        }
    }

    function
getName()  {
        return
"Joe";
    }
    function
getAge() {
        return
44;
    }
}

$person= new Person( new PersonWriter() );
$person->writeName();
$person->writeAge();
?>





Post a reply
  Related Posts  to : class with constructor parameter
 Class with a Constructor in php     -  
 Pass class instance as parameter in php     -  
 Using a Constructor in jsp     -  
 using of String constructor     -  
 Copy Constructor     -  
 What Are Constructor Methods?     -  
 cannot find symbol constructor     -  
 get url parameter     -  
 using parameter with name query     -  
 more than one parameter with name query     -  

Topic Tags

PHP OOP