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@xxxxxxxxx> schrieb im Newsbeitrag
news:49665EC3-28AF-4F97-A341-6962AB843C47@xxxxxxxxxxxx
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;
}
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php