View Single Post
Old 12-20-2009, 03:26 AM   #12 (permalink)
Jarod B
The Wanderer
 
Join Date: Aug 2009
Posts: 18
Thanks: 1
Jarod B is on a distinguished road
Default Nice, but...

Isn't there like a way where you can use these both separate? , I tried these:

...*removed*

Okay I made a little solution for the selecting weather to chain your class or not, it's fussy though, but it helps I guess :

PHP Code:
<?php
// December 19 2009 [10:02 a.m. MS]

class person {
    private 
$name;
    private 
$age;
    private 
$chain// want to chain? false (/no) by default
    
    
function __construct($chain=false) {
        
$this->chain $chain;
        
        if(
$this->chain == false) { // Set to false by default
            
return false;
        } else {
            return 
true;
        }
    }
    
    public function 
set_name($name) {
        if(
$this->chain == false) {
            return 
$this->name $name;
        } else {
            
$this->name $name;
            return 
$this;
        }
    }
    
    public function 
set_age($age) {
        if(
$this->chain == false) {
            return 
$this->age $age;
        } else {
            
$this->age $age;
            return 
$this;
        }
    }
    
    public function 
introduce() {
        if(empty(
$this->name)) {
            print(
"I need a name!");
        } else if(empty(
$this->age)) {
            print(
"I dont have an age!");
        } else {
            
printf("Hello there, my name is <b>%s</b> and I am <b>%s</b> years old."$this->name$this->age);
        }
    }
}

    
$person = new person();
    
    
$name "Jarod";
    
$age 17;
    
    echo 
"Normal Method";
    echo 
"<br />";
    echo 
$person->set_name($name) . "<br />";
    echo 
$person->set_age($age) . "<br />";
    
$person->introduce();
    echo 
"<br />";
    echo 
"<br />";
    echo 
"<br />";
    echo 
"Chaining Method";
    echo 
"<br />";
    
    
$person = new person(true); // chaining method
    
$person->set_name($name)->set_age($age)->introduce();

/** OUTPUT::>>
* Normal Method
* Jarod
* 17
* Hello there, my name is Jarod and I am 17 years old.


* Chaining Method
* Hello there, my name is Jarod and I am 17 years old.
*/
?>

Last edited by Jarod B : 12-20-2009 at 04:06 AM.
Jarod B is offline  
Reply With Quote