Php 扩展和正常调用之间有什么区别?

Php 扩展和正常调用之间有什么区别?,php,oop,class,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 Ex

两种代码之间的区别是什么

延伸

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

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

在第一个示例中,
first
扩展了
example
,因此它具有所有的方法和属性

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

我假设对于第二个示例,您需要如下内容:

Class First
{
    protected $example;

    function __construct()
    {
        $this->example = new 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实例,因此必须访问该变量才能调用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"

扩展

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

First的实例可以调用Example的方法,也可以调用自身的方法

创建示例实例

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



我相信您需要更多地接触面向对象编程,以便更多地了解其概念,并且您将理解类似的内容。

jeroen是正确的,但它还决定了示例类的哪些属性可以从第一个类访问。例如,如果示例类有两个私有或受保护的方法,并且您不进行扩展,则第一个类将无法访问它们

考虑以下几点:

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

这是一个非常基本的OOP问题。我建议首先学习类和继承的基础知识,像
class SUV extensed Car
(获取汽车的所有属性,并在此基础上进行修改)或
class SUV
(从头开始定义SUV是什么)很快,如果我想访问函数b和函数a,我需要调用示例类或第一类..?哇,太棒了,我们有时间发布响应:(@MikePurcell我们就是为了这个:)