Re: Help with OOPHP
| From: | Sebastian Hopfe | Date: | Thu, 01 Nov 2007 11:35:10 +0000 |
| Subject: | Re: Help with OOPHP | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-263909@lists.php.net to get a copy of this message | ||
Dear Andrew,
I think normaly it isn't possible to use another class in a class, without using extends. But you should use your array as a container. After you use as a container, you can make new instance into a array field.
Now you can use the content of the container to administrate the instances of the complete class. I just changed some things at your example. Please have a look and ask if you have any questions.
<?php
class fruitBasket ext
{
private $fruits = array(); //this is a class Container
public function addFruit($newFruit)
{
$this->fruits[] = new fruit($newFruit);} public function makeAllApples() {
foreach($this->fruits AS $fruit)
{
$fruit->changeName("apple");
}
}
public function showAllFruits()
{
foreach($this->fruits AS $fruit)
{
echo $fruit->showFruit()."<br>";
}
}
}
class fruit
{
private $name;
public function __construct($name)
{
$this->name = $name;} public function changeName($newName) {
$this->name = $newName;} public function showFruit() {
return $this->name;} } $Cls = new fruitBasket(); $Cls->addFruit("test1"); $Cls->addFruit("test2"); $Cls->addFruit("test3"); $Cls->addFruit("test4"); $Cls->addFruit("test5"); $Cls->addFruit("test6"); $Cls->makeAllApples(); $Cls->showAllFruits(); ?> "Andrew Peterson" <gretschdrummer@gmail.com> schrieb im Newsbeitrag news:49665EC3-28AF-4F97-A341-6962AB843C47@gmail.com...
I'm hoping you guys can help me out. I'm not sure if you can do this, but i'm trying to create a class that is build of another class. I also want to be able to do functions on the class1 from within class2. example: class fruitBasket{ private $fuit = array(); //this is a class public function addFruit($newFruit) { $this->fruitBasket[] = $newFruit(); } public makeAllApples() { foreach($this->fruit AS $value) { $value->changeName("apple"); } } } class fruit{ private $name; public __construct($name) { $this->name = $name; } public changeName($newName) { $this->name = $newName; } }