[PHP] Referencing a Class within another Class

IntactDev

Member
Nov 22, 2012
399
71
Hello DevBest.

I'm here today with a topic that has recently come to my attention. I was once trying to do a revamp of my current framework (by adding a secure backend panel). I ran into a loop of death because of this issue.

Say I have a class called "cat" and a class called "dog".

PHP:
<?php
class cat {
    function meow() {
        return 'what does the cat say?';
    }
}

class dog {
    function bark() {
        return 'what does the dog say?';
    }
}

new cat = $cat();
new dog = $dog();

class animals {
    function sounds() {
        global cat, dog; #--o here's where the issue starts! o--#
       
        echo $cat->meow() . '<br>' . $dog->bark();
    }
}

?>

With more advanced applications, it becomes one big loop when using the global function... What the the other way of calling a function inside another function?

@Kryptos @RastaLulz @Sledmore @Macemore @WhoeverElseThatsGoodAtPHP
 

Quackster

a devbest user says what
Aug 22, 2010
1,764
1,241
I'm not here to have a debate or to argue. I just want an answer. If you don't know, don't fucking reply.
If you're going to treat everyone like they don't know PHP. Then don't post a fucking thread.

--------

I tested your code, fixed the errors and it works fine.

Code:
<?php
class cat {
    function meow() {
        return 'what does the cat say?';
    }
}

class dog {
    function bark() {
        return 'what does the dog say?';
    }
}

$cat = new cat();
$dog = new dog();
$animals = new animals();
$animals->sounds();

class animals {
    function sounds() {
        global $cat, $dog;
       
        echo $cat->meow() . '<br>' . $dog->bark();
    }
}

?>
 
Last edited:

GarettM

Posting Freak
Aug 5, 2010
833
136
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.';
            }
        }
    }
?>
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;
        }
    }
?>

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;
        }
    }
?>

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();
 

Users who are viewing this thread

Top