PHP Class 扩展和对象

PHP Class Extends and Objects

我有一个 results.php 和 2 个 class 扩展宠物 class 的狗。 "fullDescription" 方法调用只是为结果返回 null(例如:"Description: Your pet is a named .")

我错过了什么?

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>

宠物Class

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

狗Class

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

}

?>

问题是因为以下几行,

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

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

您正在尝试访问 class Petprivate 属性。另外,您没有使用 setter 方法分配 属性 值。 解决方案是这样的:

您无法从 class 的子项 class 或父项 class 外部访问 class 的 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(); 

在您的 Pet class 中,您已将属性设置为 private,这意味着它们无法被操纵 publicly(来自应用程序内部),以及 only 来自设置它们的 class(在本例中为 Pet)。有什么原因吗?

将它们设置为 public:

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

这是 OOP visibility 上的手册。讨论publicprotectedprivate.

之间的区别