简体   繁体   English

PHP OOP-要求或扩展

[英]PHP OOP - Require or Extend

I was wondering if there is any major different in the following, and whether one is more 'standard' than the other: 我想知道以下内容是否有重大不同,以及一个是否比另一个更“标准”:

<?php

class Account extends Database {

    public function myMethod()
    {
        // Do something
    }

}

?>

or 要么

<?php

require('database.class.php');

class Account {

    public function myMethod()
    {
        // Do something
    }

}

?>

Cheers :) 干杯:)

Edit: 编辑:

This question actually relates to a tutorial series I have been following which describes the above two methods - which didn't make any clear sense. 这个问题实际上与我所遵循的一个教程系列有关,该系列描述了上述两种方法-并没有明确的意义。

So thank you for the constructive answers on clearing that one up! 因此,感谢您提供有关解决这一问题的建设性答案!

Both code snippets aren't even equivalent. 这两个代码段都不相同。

The first declares Account to extend Database, a is-a relation. 第一个声明Account来扩展数据库,一个is-关系。

In the second code snippet, you are simply saying that you require 'database.class.php' ... and that neither has anything to do with OO, nor defines a is-relation from Account to Database. 在第二个代码段中,您只是在说您需要 'database.class.php'...,并且既不与OO相关,也不定义从Account到Database的is-relation。

Those are two completely separate language constructs. 这是两个完全独立的语言构造。

Your first example deals with inheritance. 您的第一个示例涉及继承。 Basically, you already have a class called Database , but you want to have a specialized version of that class to handle accounts. 基本上,您已经有一个名为Database的类,但是您想拥有该类的专用版本来处理帐户。 Rather than build a brand new Account class and copy/paste all the functionality you already have in your Database class, you simply tell PHP that you want to use the existing Database class as a baseline. 与其建立一个全新的Account类并复制/粘贴您在Database类中已经具有的所有功能,您只是告诉PHP您想使用现有Database类作为基线。 You create any account-specific functionality in the new Account class, and anything database-related comes automatically. 您可以在新的Account类中创建任何特定于帐户的功能,并且所有与数据库相关的信息都会自动提供。 This is assuming, of course, that you have some way of specifying where the Database class is defined - for example, a require declaration at the top of the class, or an __autoload() or spl_autoload_register() function call defining a way to find and locate the file containing the Database class. 当然,这是假设您有某种方法可以指定Database类的定义位置-例如,该类顶部的require声明,或者__autoload()spl_autoload_register()函数调用定义了查找方法并找到包含Database类的文件。

In your second example, your database-related code is completely separated from your Account class. 在第二个示例中,与数据库相关的代码与Account类完全分开。 They're completely distinct entities, and if you wanted to do anything database-related in your Account class, you would have to explicitly instantiate a new Database object within that class (or pass it to that class, or one of its functions, as a parameter. 它们是完全不同的实体,如果您想在Account类中执行任何与数据库相关的操作,则必须在该类中显式实例化一个新的Database对象(或将其传递该类或其功能之一,例如:参数。

Basically, extends helps define what a class is, whereas require shows where a class definition (or other code) is stored. 基本上, extends有助于限定一类是什么 ,而require 表示一个类的定义(或其他代码)被存储。

Both are completely different in first one class is inherited by another class but in the second one the class is included in your script only. 两者完全不同,第一个类由另一个类继承,但是在第二个类中,该类仅包含在脚本中。

Means if you extend all the public and protected methods are available in your derived class and you can create object of derived class and can use methods with derived class's object. 意味着如果extend所有公共方法和受保护方法,都可以在派生类中使用,并且可以创建派生类的对象,并且可以将方法与派生类的对象一起使用。

But in the second method the class is included in your script and require this class it's own method and work independently. 但是在第二种方法中,该类包含在脚本中,并且需要该类是自己的方法并且可以独立工作。

The first means you create a new class, which has all the functionality of Database class and those you implement. 第一种意味着您创建一个新类,该类具有数据库类和您实现的所有功能。

The second means that you create a new class, but it doesn't have Database functionality since it's not extending it. 第二个意味着您创建了一个新类,但是它没有数据库功能,因为它没有扩展它。 If you need database access in your Account class, you can create an instance in constructor, or pass already created instance as constructor parameter. 如果需要在Account类中访问数据库,则可以在构造函数中创建实例,或将已经创建的实例作为构造函数参数传递。

It's hard to say what is more standard, since it depends on what You actually want to achieve. 很难说什么更标准,因为这取决于您实际想要实现的目标。

To put it in most simple terms:- 简单来说:

require or include is structural programming. requireinclude结构化编程。

extends is object oriented extends是面向对象的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM