PHP 在其他类中包含类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16175023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:39:02  来源:igfitidea点击:

PHP include class in other class

phpoopclassinclude

提问by Code Lover

I am learning OOP and very confuse to use classes for each other.

我正在学习面向对象编程,并且非常困惑彼此使用类。

I am having total 3 classes

我总共有 3 节课

//CMS System class
class cont_output extends cont_stacks
{
    //all methods to render the output
}


//CMS System class
class process
{
    //all process with system and db
}


// My own class to extends the system like plugin
class template_functions
{
    //here I am using all template functions
    //where some of used db query
}

Now I want to use my own class template_functions withing both system classes. But very confused how to use it. Please help me to understand this.

现在我想在两个系统类中使用我自己的类 template_functions。但是很迷茫怎么用。请帮助我理解这一点。

EDIT: I am sorry champs I forgot to mention that my own class in different PHP file.

编辑:对不起,我忘了在不同的 PHP 文件中提到我自己的类。

回答by hek2mgl

First, make sure, that you includethe class file before using it:

首先,请确保include在使用类文件之前使用它:

include_once 'path/to/tpl_functions.php';

This should be done either in your index.php or on top of the class which uses tpl_function. Also note the possibility of autoloadingclasses:

这应该在您的 index.php 中或在使用tpl_function. 还要注意autoloading类的可能性:

Since PHP5 you have to possibility to autoload classes. This means you register a hook function that is been called everytime when you try to use a class which's code file hasn't been included yet. Doing it you won't need to have include_oncestatements in every class file. Here comes an example:

从 PHP5 开始,您必须能够自动加载类。这意味着您注册了一个钩子函数,当您尝试使用尚未包含其代码文件的类时,每次都会调用该函数。这样做你不需要include_once在每个类文件中都有语句。下面是一个例子:

index.phpor whatever application entry point:

index.php或任何应用程序入口点:

spl_autoload_register('autoloader');

function autoloader($classname) {
    include_once 'path/to/class.files/' . $classname . '.php';
}

From now on you can access the classes without to worry about including the code files anymore. Try it:

从现在开始,您可以访问这些类而不必再担心包含代码文件了。尝试一下:

$process = new process();


Knowing this, there are several ways how you can use the template_functionsclass

知道了这一点,有几种方法可以使用template_functions该类



Just use it:

只需使用它

You can access the class in any part of the code if you create an instance of it:

如果创建它的实例,则可以在代码的任何部分访问该类:

class process
{
    //all process with system and db

    public function doSomethging() {
        // create instance and use it
        $tplFunctions = new template_functions();
        $tplFunctions->doSomethingElse();
    }
}

Instance members:

实例成员:

Take the process class for example. To make the template_functions available inside the processclass, you create an instance member and initialize it somewhere, where you need it, the constructor seems to be a good place:

以进程类为例。为了使类中的 template_functions 可用process,您创建一个实例成员并在需要它的地方初始化它,构造函数似乎是一个好地方:

//CMS System class
class process
{
    //all process with system and db

    // declare instance var
    protected tplFunctions;

    public function __construct() {
        $this->tplFunctions = new template_functions;
    }

    // use the member : 

    public function doSomething() {
        $this->tplFunctions->doSomething();
    }


    public function doSomethingElse() {
        $this->tplFunctions->doSomethingElse();
    }
}

回答by Tim

You can extend the template_functionsclass, then you can use all the functions.

您可以扩展template_functions该类,然后您可以使用所有功能。

class cont_output extends cont_stacks //cont_stacks has to extend template_functions
{
    public function test() {
        $this->render();
    }
}


class process extends template_functions
{ 
    public function test() {
        $this->render();
    }
}


class template_functions
{
    public function render() {
        echo "Works!";
    }
}