Jump to content

abstract class help


gomesla

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.