Re: Re: Help with OOPHP (SOLVED)

From: Date: Thu, 01 Nov 2007 17:15:44 +0000
Subject: Re: Re: Help with OOPHP (SOLVED)
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-263938@lists.php.net to get a copy of this message
I've figured it out :) Thanks for the help, I just need to walk away for a minute and come back to it. all I need to do is this: myClass.php ---------------------- <?PHP require mySecondClass.php; class myClass { /* Now I can create/edit/maninpulate/etc new and old instances of "mySecondClass" */ } ?> mySecondClass.php ------------------- <?PHP mySecondClass { //constuct, functions, etc.... } ?> On Nov 1, 2007, at 7:35 AM, Sebastian Hopfe wrote:
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; } }
--PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#263938) next »