0

I've been programming mainly with JAVA and have written procedural programs in PHP, but now I'm try to write some OOP bases programs in PHP and I'm facing a problem. I've got two files , Zoo.php and Dog.php , each contains a class.

Dog.php:

<?php
class Dog {
     private $name;
     private $color;

     public function __construct($name,$color) {
         $this->name = $name;
         $this->color = $color;
     }

     public function getName(){
         return $this->name;
     }

     public function getColor(){
         return $this->color;
     }
}

And Zoo.php:

<?php

class Zoo {
    private $name;
    private $dogs;

    public function __construct($name) {
        $this->name = $name;
        $dogs = array();
    }

    public function addDog($dogName,$dogColor){
        $dog = new Dog($dogName,$dogColor);
        array_push($this->dogs,$dog);
    }

    public function getAllDogs(){
        var_dump($dogs);
    }
}

echo "start";
$z = new Zoo("test_zoo");
$z->addDog("blackie","black");
$z->getAllDogs();

The code above outputs :

Fatal error: Class 'Dog' not found in C:\wamp\www\Zoo.php on line 13

I'd like to know what's wrong with the code above and how creating an object instance within another object should be done in PHP. Thanks in advance.

1

4 Answers 4

1

I gues you are not including Dog class.

<?php
include "Dog.php";

class Zoo {
/* ... */
}

Or you can use autoloading to auto include any class by default.

Sign up to request clarification or add additional context in comments.

Comments

0

It's not about OOP. Just you forgot to include Dog file to use it you Zoo class :

<?php

include 'Dog.php'; // or whatever path   

class Zoo { ...

Everything else should be ok and seems to be a good use of PHP OOP btw. :)

Comments

0

You haven't included your Dog.php in your Zoo.php. Therefore you can't create an new instance.

Just above class Zoo add this:

include("Dog.php");
class Zoo {

Comments

0

All you need to return function . Three mistakes here, u'll see all of them in my code . Constructer nothing return , behave like void , so we need a method that returns dog specification.If you wanna use the classes in separate file, so u've to use php method that "include" or "require" or "require_once" etc ( see the difference of this method)

<?php 

class Dog {
    private $name;
    private $color;

    public function __construct($name,$color) {
        $this->name = $name;
        $this->color = $color;
    }

    public function getName(){
        return $this->name;
    }

    public function getColor(){
        return $this->color;
    }
    public function getDogs(){
        return array("name"=>$this->name,"color"=>$this->color);

    }
}


class Zoo extends Dog{
    private $name;
    private $dogs=array();

    public function __construct($name) {
        $this->name = $name;
        $dogs = array();
    }

    public function addDog($dogName,$dogColor){
        $dog = new Dog($dogName,$dogColor);
        array_push($this->dogs,$dog->getDogs());
    }

    public function getAllDogs(){
        var_dump($this->dogs);
    }
}

echo "start";
$z = new Zoo("test_zoo");
$z->addDog("blackie","black");

$z->getAllDogs();
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.