Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I have been given some code that has a class with functions in it. I understand some of it but I'm having trouble creating a new instance (a person) in the example code below.

I was wondering if somebody could show me how it is done using the code that is used in the other examples in the code below, and on which line your code would go.

useanimal.php:

PHP
<?php

// read in the class definition file
require_once('animal.class.php');

// create an new instance of the dog class
$dog = new Dog("Spot");
$dog->setAge(5);
// use both the dog methods and the dog attributes - notice when you run this that the dog method 'getAge()' will say what the equivalent in human years is whereas the cat won't.
echo "<p>my name is ".$dog->getName()." and I am a ".$dog->type. ", ".$dog->getAge()." and ".$dog->speak()."</p>";

// create a new instance of the cat class
$cat = new Cat("Tigger");
$cat->setAge(2);
// use the cat methods and attributes
echo "<p>my name is ".$cat->getName()." and I am a ".$cat->type. ", ".$cat->getAge(). " and ".$cat->speak()."</p>";

// make yet another cat, quite different from the first cat - the two cats are separate instances
$anotherCat = new Cat("Tibbles");
$anotherCat->setAge( 7 );
// make this cat say who and what it is
echo "<p>my name is ".$anotherCat->getName()." and I am a ".$anotherCat->type. ", ".$anotherCat->getAge(). " and ".$anotherCat->speak()."</p>";

?>


and the other file is animal.class.php:

PHP
<?php

abstract class Animal {
    protected $sound;
    protected $movement;
    public $type;
    protected $name;
    protected $age;

    protected function __construct( $name = null, $type=null, $movement=null, $sound=null ) {
        // assign to the class variables the value in the local variable
        $this->setName( $name );
        $this->type = $type;
        $this->movement = $movement;
        $this->sound = $sound;
    }

    // common methods that the concrete classes can override or use as is.
    function move() {
        return "I move by {$this->movement}";
    }

    function speak() {
        return "I say {$this->sound}";
    }

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

    public function setAge( $age ) {
        $this->age = $age;
    }

    public function getAge() {
        return "I am {$this->age} years old";
    }
}

/**
 *
 * @package UseClasses
 * A concrete class that will inherit from animal
 */
class Dog extends Animal {

    function __construct( $name = null ) {
        /** call the parent constructor rather than re-write it.  Notice in the Cat class
         * we re-write the constructor - not normally good practice of course to duplicate the code
         * I do it in the Cat class just to demonstrate that you can
         */
        parent::__construct( $name, 'Dog', 'walking and running', 'woof, woof' );
    }

    /** override the animal function to add how many human years the age is equivalent to.
     * This is the advantage of using a method to get/set an attribute - you can convert, manipulate
     * and alter what is actually stored within the class, the client only sees the 'view' the get/sets provide
     */
    public function getAge() {
        return "I am {$this->age} years old, that's " . $this->age * 7 . " human years";
    }
}





/**
 *
 * @package UseClasses
 */
class Cat extends Animal {

    function __construct( $name = null ) {
        /** here we re-write the parent constructor - normally you only do this if
         * there are particular things you want this concrete class to do.
         * Frequently a specialised class will call the parent constructor (parent::__construct)
         * and then, following that, do any other initialisation it needs to
         */
        $this->setName( $name );
        $this->type = 'Cat';
        $this->movement = "slinking and sliding";
        $this->sound = "meiou";
    }
}

?>


Any guidance is much appreciated
Posted
Comments
Richard MacCutchan 1-Oct-14 9:40am    
First you need to create a Person class.
Sergey Alexandrovich Kryukov 1-Oct-14 12:47pm    
A Cat person or who? What's the problem? Are those classes yours? If not, create yours.
—SA
jba1991 1-Oct-14 12:53pm    
I need to create a person class the same way a dog class has been created but I am unfamiliar in how to do it. I've tried similar ways to the dog class and the cat class but because it passes different parameters (gender, height instead of movement and sound) i think I am doing it wrong.

These classes arent mine but ive been tasked with understanding how to create a new instance for a person class.
Sergey Alexandrovich Kryukov 1-Oct-14 13:01pm    
This is a matter of OOD, a matter of understanding of driving forces of OOP. You need to derive all classes, from, say, Animal. We are all animals, aren't we?
—SA
jba1991 1-Oct-14 13:30pm    
I have just created a new abstract class for Human. Then I will create a new class which extends Human called Person. I will comment what I have added below.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900