0

I have 4 files index.php, Database.php, Zoo.php, Animal.php

// Zoo.php

class Zoo {
    private $db;
    private $animals = array();

    public function __constructor(Database $db) {
        $this->db = $db;
        $this->getAllAnimals();
    }

    private function getAllAnimals() {
        //Database stuff, returns an array with all animals
        $p=0;
        foreach($dbresult as $an){
            $animals[$p++] = new Animal($an['name'], $an['age'], $an['weight']);
        }
    }

    public function listAnimals() {
        foreach ($this->animals as $a){
            echo $a->name;
            //and so on
        }
    }
}

// Animal.php

class Animal {
    // variables for the animals
}

// index.php

<?php
    include 'Database.php';
    include 'Zoo.php';

    $db = new Database();
    $zoo = new Zoo($db);
    $zoo->listAnimals();
?>  

This is from the top of my head, so if there are some errors, just treat it as pseudocode :)

My problem:
I get a Fatal Error Class Animal not found. If I add include 'Animal.php'; in the first line of Zoo.php, right before class Zoo { it works.

I'm stil learning about OOP with php, and the include-line strikes me as odd, so I ask for someone to help me out with this.
Is there another way to use "Animal"-objects in the "Zoo"-class, without the include or is it normal to use include, or maybe require/require_once?

2
  • This is from the top of my head, so if there are some errors, just treat it as pseudocode :) No. Post the actual testcase you're using for debugging. Commented Feb 22, 2012 at 12:48
  • 1
    the include-line strikes me as odd Why's that, then? Commented Feb 22, 2012 at 12:48

3 Answers 3

1

If you need the "Animal" class inside Zoo.php, require_once("Animal.php"); at the top of Zoo.php. If you need it in some other file, do the same over there.

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

Comments

1

I believe that most OOP developers these days take advantage of __autoload or (even better) the SPL autoloader, even if only in the libraries and frameworks that they use.

4 Comments

Start by looking at the number of frameworks and libraries that use autoloaders
That's not necessarily "most OOP developers"
In that case, I retract my "most", and replace it with "I believe that most"... though there's probably (in my opinion) more than a few people using Drupal or Symfony or Zend or any of a myriad of libraries and frameworks that use autoloaders. I believe that there are PHP developers who do use these
Better ;) I can probably agree with you now
0

the include-line strikes me as odd

Why's that, then? To use Animal you must include its definition. Always seemed pretty rational to me.

There are alternatives such as require and require_once that do the same thing (but with some added restrictions), and some more exotic alternatives such as autoloading. But for simple tasks, include will do just fine.

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.