PHP类扩展和对象

时间:2016-05-01 15:35:57

标签: php class extends

我有一个results.php和2个类,Dog扩展了Pet类。 " fullDescription"方法调用只是为结果返回null(例如:"描述:你的宠物是一个名字。")

我错过了什么?

results.php:

    <?php
    //include('_includes/pet.class.php');
    include("_includes/dog.class.php");

?>
<?php

// COLLECT THE VALUES FROM THE FORM
$petType = $_POST["petType"];
$petName = $_POST["petName"];

// CREATE A NEW INSTANCE OF THE CORRECT TYPE
if ($petType == "dog")
{
    $myPet = new Dog();
    $myPet->breed = Dog::randomBreed();
}
else
{
    $myPet = new Cat();
    $myPet->breed = randomBreed();
}

// ASSIGN THE VALUE FROM THE FORM TO THE name PROPERTY OF THE PET OBJECT
$myPet->name = $petName;
$myPet->descriptor = Pet::randomDescriptor();
$myPet->color = Pet::randomColor();

?>

<div class="basic-grey">

<h1>Here's the information about your pet:</h1>

<p>Pet Name:  <?php echo $myPet->name; ?></p>
<p>Pet Name:  <?php echo $myPet->breed; ?></p>
<p>Pet Name:  <?php echo $myPet->color; ?></p>
<p>Pet Name:  <?php echo $myPet->descriptor; ?></p>
<p>Description: <?php echo $myPet->fullDescription(); ?> </p>

宠物等级

<?php

class Pet
{

    // DEFINE YOUR CLASS PROPERTIES HERE
    private $name;
    private $descriptor;
    private $color;
    private $breed;

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

    public function setDescriptor($descriptor) {
        $this->descriptor = $descriptor;
    }
    public function getDescriptor() {
        return $this->descriptor;
    }

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

    public function setBreed($breed) {
        $this->breed = $breed;
    }
    public function getBreed() {
        return $this->breed;
    }

    // DEFINE YOUR METHODS HERE
    public function fullDescription()
    {
        return "Your pet is a $this->descriptor $this->color $this->breed named $this->name.";
        //echo $myPet->getName();
    }

    public static function randomDescriptor()
    {
        // SET UP AN ARRAY OF VALUES
        $input = array("stinky", "huge", "tiny", "lazy", "lovable");

        // RETURN A SINGLE RANDOM ELEMENT FROM THE ARRAY
        return array_rand(array_flip($input), 1);
    }

    public static function randomColor()
    {
        // SET UP AN ARRAY OF VALUES
        $input = array("tan", "brown", "black", "white", "spotted");

        // RETURN A SINGLE RANDOM ELEMENT FROM THE ARRAY
        return array_rand(array_flip($input), 1);
    }
}
?>

狗类

<?php
include('pet.class.php');

//////////// DOG CLASS //////////////
class Dog extends Pet
{public static function randomBreed()
    {
        // SET UP AN ARRAY OF VALUES
        $input = array("german shepherd", "dachsund", "retriever", "labradoodle", "bulldog");

        // RETURN A SINGLE RANDOM ELEMENT FROM THE ARRAY
        return array_rand(array_flip($input), 1);
    }

}

?>

2 个答案:

答案 0 :(得分:1)

问题在于以下几行,

$myPet->breed = Dog::randomBreed();

$myPet->name = $petName;
$myPet->descriptor = Pet::randomDescriptor();
$myPet->color = Pet::randomColor();

您正在尝试访问privatePet的特性。另外,您没有使用setter方法赋值属性值。 解决方案将是这样的:

您无法从其子类或父类之外访问类的private属性。首先将它们声明为protected属性:

class Pet
{

    // DEFINE YOUR CLASS PROPERTIES HERE
    protected $name;
    protected $descriptor;
    protected $color;
    protected $breed;

    // your code

}

然后在 results.php 页面按以下方式更改这些行:

// your code

if ($petType == "dog")
{
    $myPet = new Dog();
    $myPet->setBreed(Dog::randomBreed());
}
else
{
    $myPet = new Cat();
    $myPet->setBreed(Cat::randomBreed());
}
$myPet->setName($petName);
$myPet->setDescriptor(Pet::randomDescriptor());
$myPet->setColor(Pet::randomColor());
echo $myPet->fullDescription(); 

答案 1 :(得分:0)

Pet课程中,您已将属性设置为private,这意味着无法公开公开(来自应用程序内),仅< / em>来自设置它们的类(在本例中为Pet)。有什么理由吗?

将它们设置为public

public $name;
public $descriptor;
public $color;
public $breed;

以下是OOP visibility上的手册。讨论 public protected private 之间的差异。