php - Calling my objects and its functions -


i new php , trying using factory method. want instantiate class shapes using 'build' function,such when class 'circle' instantiated area , circumference calculated , if class 'rectangle' called ,its area , circumference calculated , returned. have far , want achieve.this doesnt work , sure there better way of doing it!

$allmyshapes = shapes::build($initdata);  foreach($allmyshapes $value) {    if ($value =='circle'){    $x = new circle();    echo $x::area();    echo $x::circumference(); }  if ($value =='rectangle'){    $y = new rectangle();    echo $y::area();    echo $y::circumference();  } 

}

my class:

class shapes {   public static function build($initdata){      }     foreach($newlist $value){        if($value[0]=='circle'){           $shape1 =new circle($value[1],$value[2]);         }        if($value[0]== 'rectangle'){          $shape2 =new rectangle($value[1],$value[2]);          }      }      return array($shape1,$shape2);   } 

}

class circle extends shapes {   public $radius;   public $centre_point;   public function __construct($radius, $centre_point) {     $this->radius = $radius;     $this->centre_point = $centre_point;   }   public function area(){     return (pi() *$this->radius * $this->radius);   }   public function circumference(){     return 2 * pi() *$this->radius;   }   }  class rectangle extends shapes {   public $x;   public $y;   public function __construct($x, $y) {     $this->x= $x;     $this->y = $y;   }   public function area() {     return $this->x * $this->y;   }   public function circumference() {     return 2 * ($this->x+ $this->y);   } 

}

there's nothing wrong classes, how using them output information:

class shapes {     public static function build($initdata){         //$initdata heredoc         $newlist = explode("\n", $initdata);         foreach($newlist $key => $initdata){             $newlist[$key] = explode("\t", $initdata);         }         foreach($newlist $value){             if($value[0] == 'circle'){                 $shape1 = new circle($value[1], $value[2]);             }             if($value[0] == 'rectangle'){                 $shape2 = new rectangle($value[1], $value[2]);             }         }         return array($shape1, $shape2);     } }  class circle extends shapes {     public $radius;     public $centre_point;     public function __construct($radius, $centre_point){         $this->radius = $radius;         $this->centre_point = $centre_point;     }     public function area(){         return (pi() * $this->radius * $this->radius);     }     public function circumference(){         return 2 * pi() * $this->radius;     }     public function draw(){         return ( $this->radius.":".$this->centre_point);     } }  class rectangle extends shapes {     public $x;     public $y;     public function __construct($x, $y){         $this->x = $x;         $this->y = $y;     }     public function area(){         return $this->x * $this->y;     }     public function circumference(){         return 2 * ($this->x + $this->y);     } }  $initdata = <<<endinit circle  5   2 rectangle   5   10 ellipse 10 10   4   5 endinit; $allmyshapes = shapes::build($initdata); foreach($allmyshapes $value){     if(get_class($value) === 'circle'){         echo nl2br("**circle**\narea: ".$value->area()."\ncircumference: ".$value->circumference()."\n");     }     if(get_class($value) === 'rectangle'){          echo nl2br("**rectangle**\narea: ".$value->area()."\ncircumference: ".$value->circumference()."\n");     } } 

i added 2 functions provided code, get_class() & nl2br(). nl2br can removed if using in cli format, main function output readability when using web browser view output.

get_class() solve issue of trying figure out constructor used in creation of object.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -