Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从另一个对象调用对象_Php_Oop - Fatal编程技术网

PHP从另一个对象调用对象

PHP从另一个对象调用对象,php,oop,Php,Oop,我主要是用JAVA编程,用PHP编写过程程序,但现在我试图用PHP编写一些面向对象的程序,我面临一个问题。 我有两个文件,Zoo.php和Dog.php,每个文件都包含一个类 Dog.php: <?php class Dog { private $name; private $color; public function __construct($name,$color) { $this->name = $name;

我主要是用JAVA编程,用PHP编写过程程序,但现在我试图用PHP编写一些面向对象的程序,我面临一个问题。 我有两个文件,
Zoo.php
Dog.php
,每个文件都包含一个类

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

我想知道上面的代码有什么问题,以及在PHP中如何在另一个对象中创建对象实例。提前谢谢。

这与OOP无关。只是你忘了在你的动物园课程中包含狗文件:

<?php

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

class Zoo { ...

您的
Zoo.php
中没有包含您的
Dog.php
。因此,无法创建新实例

在类动物园上方添加以下内容:

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

我想你不包括狗课

<?php
include "Dog.php";

class Zoo {
/* ... */
}

返回函数所需的全部内容。
这里有三个错误,您将在我的代码中看到它们。构造函数nothing return,行为类似于void,因此我们需要一个返回dog规范的方法。如果你想在单独的文件中使用类,那么你必须使用“include”或“require”或“require_once”等php方法(参见此方法的区别)


必须在zoo.php中包含dog.php,以便调用时该类可用。您可能需要查看自动加载类-
include("Dog.php");
class Zoo {
<?php
include "Dog.php";

class Zoo {
/* ... */
}
<?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();
?>