gomesla Posted September 10, 2008 Share Posted September 10, 2008 Hello! New to PHP and I'm trying to learn about abstract classes. I defined a basic class and method but when I split the class into an abstract class and defined the method in the child class it give me " Warning: Invalid argument supplied for foreach() " Here is the code without the abstract class (works fine): <?php class ShopProduct { private $title; private $producerMainName; private $producerFirstName; private $price; private $discount = 0; public function __construct( $title, $firstname, $mainname, $price ) { $this->title = $title; $this->producerMainName = $firstname; $this->producerFirstName = $mainname; $this->price = $price; } public function getTitle() { return $this->title; } public function getProducer() { return "{$this->producerFirstName} ". "{$this->producerMainName}"; } } class XmlProductWriter { private $products = array(); public function addProduct( ShopProduct $shopProduct ) { $this->products[] = $shopProduct; } public function write() { $str = ""; foreach ( $this->products as $shopProduct ) { $str .= "{$shopProduct->getTitle()}: "; $str .= $shopProduct->getProducer(); $str .= "<br />"; } print $str; } } $product4 = new ShopProduct ( "Learn PHP", "Pitt", "Brad", "14.99"); $product5 = new ShopProduct ( "Learn MySQL", "Clooney", "George", "18.99"); // New Object of the XmlProductWriter Class. $manyProducts = new XmlProductWriter(); //Adding ShopProduct objects to the Array inside XmlProductWriter Class object. $manyProducts->addProduct($product4); $manyProducts->addProduct($product5); //Print the list of objects inside the array. $manyProducts->write(); ?> Here is the code with the abstract class (gives error): <?php class ShopProduct { private $title; private $producerMainName; private $producerFirstName; private $price; private $discount = 0; public function __construct( $title, $firstname, $mainname, $price ) { $this->title = $title; $this->producerMainName = $firstname; $this->producerFirstName = $mainname; $this->price = $price; } public function getTitle() { return $this->title; } public function getProducer() { return "{$this->producerFirstName} ". "{$this->producerMainName}"; } } abstract class ShopProductWriter { private $products = array(); public function addProduct( ShopProduct $shopProduct ) { $this->products[] = $shopProduct; } abstract public function write(); } class XmlProductWriter extends ShopProductWriter { public function write() { $str = ""; foreach ( $this->products as $shopProduct ) { $str .= "{$shopProduct->getTitle()}: "; $str .= $shopProduct->getProducer(); $str .= "<br />"; } print $str; } } $product4 = new ShopProduct ( "Learn PHP", "Pitt", "Brad", "14.99"); $product5 = new ShopProduct ( "Learn MySQL", "Clooney", "George", "18.99"); // New Object of the XmlProductWriter Class. $manyProducts = new XmlProductWriter(); //Adding ShopProduct objects to the Array inside XmlProductWriter Class object. $manyProducts->addProduct($product4); $manyProducts->addProduct($product5); //Print the list of objects inside the array. $manyProducts->write(); ?> Thanks for the help!!!! Link to comment https://forums.phpfreaks.com/topic/123541-abstract-class-help/ Share on other sites More sharing options...
DarkWater Posted September 10, 2008 Share Posted September 10, 2008 What error does it give? Link to comment https://forums.phpfreaks.com/topic/123541-abstract-class-help/#findComment-638042 Share on other sites More sharing options...
gomesla Posted September 10, 2008 Author Share Posted September 10, 2008 It gives me: Warning: Invalid argument supplied for foreach() Link to comment https://forums.phpfreaks.com/topic/123541-abstract-class-help/#findComment-638063 Share on other sites More sharing options...
gomesla Posted September 10, 2008 Author Share Posted September 10, 2008 I'm still having trouble with this. Does anybody know why the error? ??? Link to comment https://forums.phpfreaks.com/topic/123541-abstract-class-help/#findComment-638469 Share on other sites More sharing options...
Mchl Posted September 10, 2008 Share Posted September 10, 2008 You have a private property in an abstract class. Link to comment https://forums.phpfreaks.com/topic/123541-abstract-class-help/#findComment-638472 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.