Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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/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
PHP在其他类中包含类_Php_Oop_Class_Include - Fatal编程技术网

PHP在其他类中包含类

PHP在其他类中包含类,php,oop,class,include,Php,Oop,Class,Include,我正在学习面向对象编程,很难相互使用类 我总共有三节课 //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 p

我正在学习面向对象编程,很难相互使用类

我总共有三节课

//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
}
现在我想在两个系统类中使用我自己的类模板函数。但是很困惑如何使用它。请帮助我理解这一点

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

首先,确保在使用类文件之前
包含该类文件:

include_once 'path/to/tpl_functions.php';
这应该在index.php中完成,或者在使用
tpl_函数的类的顶部完成。还应注意以下类别的可能性:

由于PHP5,您必须能够自动加载类。这意味着您要注册一个钩子函数,每当您试图使用一个尚未包含代码文件的类时,该钩子函数都会被调用。这样,您就不需要在每个类文件中都包含一次
include_
语句。下面是一个例子:

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

spl_autoload_register('autoloader');

function autoloader($classname) {
    include_once 'path/to/class.files/' . $classname . '.php';
}
从现在起,您可以访问这些类,而无需再担心包含代码文件。试试看:

$process = new process();

了解这一点,有几种方法可以使用
template\u函数


只要使用它:

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

class process
{
    //all process with system and db

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

以process类为例。为了使template_函数在
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();
    }
}

您可以扩展
template\u函数
类,然后可以使用所有函数

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

我对你们的困惑感到困惑…听起来你们想要多重继承,所以cont_输出同时扩展cont_堆栈和template_函数?因为否则,类#1就没有理由不能调用静态方法,也没有理由不能在自身内部实例化类#2的副本。@MarcB这有点像,但取决于我如何在两个类中使用自己的类。感谢您的快速响应。。再问几句就好了。。protected是一个变量,它是否需要
$
?我忘了在单独的php文件中提到我的类,所以在哪里包含我的文件?我将给出一个自动加载的示例。。给我几分钟。你应该知道这件事……好吧。无论如何,您都应该阅读有关自动加载的内容,因为它是php的一个很酷的功能!)当然我会的。。我真的需要了解所有这些。。非常感谢,但是关于
类进程
呢?很抱歉,但我试着去理解