Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
[PHP] Referencing a Class within another Class
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="GarettM" data-source="post: 263147" data-attributes="member: 839"><p>Way OFF TOPIC:</p><p>You should try this approach </p><p></p><p>animals or index.php</p><p>[PHP]<?php</p><p></p><p> /**</p><p> * Animals class</p><p> */</p><p> </p><p> class Animals</p><p> {</p><p> protected $animal;</p><p> </p><p> public function getAnimal($animal)</p><p> {</p><p> if(is_readable("animals/$animal.php"))</p><p> {</p><p> require("animals/$animal.php");</p><p> $this->animal = new $animal();</p><p> }</p><p> }</p><p> public function getInfo()</p><p> {</p><p> if(is_object($this->animal))</p><p> {</p><p> printf(</p><p> 'Animal: %s <br />\n Sound: %s <br />\n Type: %s <br />\n'</p><p> , $this->animal->getName, $this->animal->getSound(), $this->getType()</p><p> );</p><p> } else {</p><p> return 'No Animal Found.';</p><p> }</p><p> }</p><p> }</p><p>?>[/PHP]</p><p>Cat</p><p>[PHP]</p><p><?php</p><p> /**</p><p> * Cat</p><p> */</p><p> </p><p> class Cat</p><p> {</p><p> protected $sound, $name, $type;</p><p> </p><p> public function __construct()</p><p> {</p><p> $this->name = 'Cat';</p><p> $this->type = 'House Pet';</p><p> $this->sound = 'Meow';</p><p> }</p><p> public function getSound()</p><p> {</p><p> return $this->sound;</p><p> }</p><p> public function getName()</p><p> {</p><p> return $this->name;</p><p> }</p><p> public function getType()</p><p> {</p><p> return $this->type;</p><p> }</p><p> }</p><p>?> </p><p>[/PHP]</p><p></p><p>Dog</p><p>[PHP]</p><p></p><p><?php</p><p> /**</p><p> * Dog</p><p> */</p><p> </p><p> class Dog</p><p> {</p><p> protected $sound, $name, $type;</p><p> </p><p> public function __construct()</p><p> {</p><p> $this->name = 'Dog';</p><p> $this->type = 'House Pet';</p><p> $this->sound = 'Ruff';</p><p> }</p><p> public function getSound()</p><p> {</p><p> return $this->sound;</p><p> }</p><p> public function getName()</p><p> {</p><p> return $this->name;</p><p> }</p><p> public function getType()</p><p> {</p><p> return $this->type;</p><p> }</p><p> }</p><p>?></p><p>[/PHP]</p><p></p><p>This is more Object Orientated i believe not sure </p><p>initialize it</p><p>[PHP]</p><p><?php</p><p></p><p> /**</p><p> * Initialize</p><p> */</p><p> </p><p> $animals = new Animals;</p><p> </p><p> $animals->getAnimal('Cat');</p><p> $animals->getInfo();</p><p> </p><p> $animals->getAnimal('Dog');</p><p> $animals->getInfo();</p><p>[/PHP]</p></blockquote><p></p>
[QUOTE="GarettM, post: 263147, member: 839"] Way OFF TOPIC: You should try this approach animals or index.php [PHP]<?php /** * Animals class */ class Animals { protected $animal; public function getAnimal($animal) { if(is_readable("animals/$animal.php")) { require("animals/$animal.php"); $this->animal = new $animal(); } } public function getInfo() { if(is_object($this->animal)) { printf( 'Animal: %s <br />\n Sound: %s <br />\n Type: %s <br />\n' , $this->animal->getName, $this->animal->getSound(), $this->getType() ); } else { return 'No Animal Found.'; } } } ?>[/PHP] Cat [PHP] <?php /** * Cat */ class Cat { protected $sound, $name, $type; public function __construct() { $this->name = 'Cat'; $this->type = 'House Pet'; $this->sound = 'Meow'; } public function getSound() { return $this->sound; } public function getName() { return $this->name; } public function getType() { return $this->type; } } ?> [/PHP] Dog [PHP] <?php /** * Dog */ class Dog { protected $sound, $name, $type; public function __construct() { $this->name = 'Dog'; $this->type = 'House Pet'; $this->sound = 'Ruff'; } public function getSound() { return $this->sound; } public function getName() { return $this->name; } public function getType() { return $this->type; } } ?> [/PHP] This is more Object Orientated i believe not sure initialize it [PHP] <?php /** * Initialize */ $animals = new Animals; $animals->getAnimal('Cat'); $animals->getInfo(); $animals->getAnimal('Dog'); $animals->getInfo(); [/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
[PHP] Referencing a Class within another Class
Top