扩展和正常呼叫之间有什么区别?

时间:2012-01-21 22:03:53

标签: php oop class

两个代码之间有什么区别?

扩展;

<?php 
require_once 'example.class.php';
Class First extends Example
{}
?>

正常通话;

<?php
require_once 'example.class.php';
Class First
{
    public $example;
    function __construct()
    {
        $this->example = new Example();
    }
}
?>

我知道使用受保护的pharase的一些区别。 但在我看来这还不够。

4 个答案:

答案 0 :(得分:3)

第一个,对象First将具有与Example类相同的属性/功能。像:

class Example
{
    public function a()
    {
    }
}

class First extends Example
{
    public function b()
    {
    }
}

如果您实例化两个对象$ ex1,$ ex2:

$ex1 = new Example();
$ex1->a(); // this is valid
$ex1->b(); // this is invalid because Example doesn't have "b" function

$ex2 = new First();
$ex2->a(); // this is valid
$ex2->b(); // this is valid too, because First inherits Example members + its own

在第二个代码上,您正在创建一个示例实例,因此您必须访问该变量才能调用Example方法。

一个更好的例子:

class Person
{
    public $name;

    public function say($message) 
    { 
        echo $this->name . " says " . $message;
    }
}

class Teacher extends Person
{       
    public function say($message) 
    { 
        // note that Teacher has a name even this is not declared here.
        echo $this->name . " says " . $message; 
    }

    public function teach($what) 
    { 
        // note that Teacher has a name even this is not declared here.
        echo $this->name . " is teaching " . $what; 
    }
}

见输出:

$john = new Person();
$john->name = "John Doe";
$john->say("hello world!");
/* 
$john->teach("Portuguese"); // invalid, person doesn't teach anything.
*/ 


$chuck = new Teacher();
$chuck->name = "Chuck Norris";
$chuck->say("hello universe!");
$chuck->teach("Fighting"); // valid because Teacher has method "teach"

答案 1 :(得分:1)

<强>扩展

这是对象继承。 首先继承示例成员,因此首先 示例

First 的一个实例可以调用示例的方法和其中一个方法。

创建示例

的实例

这只是创建示例的对象。 第一个方法可以使用其他对象来实现其目标。


我相信你需要更多地了解面向对象的编程,以便更多地了解它的概念,你会理解这样的事情。

答案 2 :(得分:0)

在第一个示例中,First扩展了Example,因此它具有所有方法和属性。

在第二个示例中,您只是将属性设置为类Example的对象。至少我认为这是你想要做的,因为你写它的方式,$example只在构造函数的范围内定义,因此它永远不会在任何地方可用。

我认为对于你的第二个例子你会想要这样的东西:

Class First
{
    protected $example;

    function __construct()
    {
        $this->example = new Example();
    }
}

答案 3 :(得分:0)

jeroen是正确的,但它也确定可以从First类访问Example类的哪些属性。例如,如果Example类有2个私有或受保护的方法,并且您没有扩展,则First类将无法访问它们。

请考虑以下事项:

class Example
{
    public $foo;
    protected $bar;
}

Class First extends Example
{
    public function __construct()
    {            
        $this->foo = 'FOO1'; // Works because public scope
        $this->bar = 'BAR1'; // Works even though scope is protected, because we extended the class
    }
}

Class Second
{
    public example;

    function __construct()
    {
        $this->example = new Example();

        $this->example->foo = 'FOO1'; // Works because public scope
        $this->example->bar = 'BAR2'; // Fails because protected scope and we did not extend the class
    }
}

// However, from the calling code, I am also limited
$first = new First();

$first->foo = 'NEW_FOO1'; // Works because public scope
$first->bar = 'NEW_BAR1'; // Fails because protected scope

$second = new Second();

$second->example->foo = 'NEW_FOO2'; // Works because public scope
$second->example->bar = 'NEW_BAR2'; // Fails because protected scope