PHP包括其他类中的类
发布时间:2020-12-13 18:15:44 所属栏目:PHP教程 来源:网络整理
导读:我正在学习OOP,并且非常混淆彼此使用类. 我总共有三节课 //CMS System classclass cont_output extends cont_stacks{ //all methods to render the output}//CMS System classclass process{ //all process with system and db}// My own class to extends t
|
我正在学习OOP,并且非常混淆彼此使用类.
我总共有三节课 //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
}
现在我想使用我自己的类template_functions和两个系统类.但很困惑如何使用它.请帮我理解这个. 编辑:
首先,确保在使用之前包含类文件:
include_once 'path/to/tpl_functions.php'; 这应该在index.php中或在使用tpl_function的类的顶部完成.还要注意 从PHP5开始,你必须自动加载类.这意味着您注册了一个钩子函数,当您尝试使用尚未包含代码文件的类时,该函数每次都被调用.这样做你不需要在每个类文件中都有include_once语句.这是一个例子: index.php或任何应用程序入口点: spl_autoload_register('autoloader');
function autoloader($classname) {
include_once 'path/to/class.files/' . $classname . '.php';
}
从现在开始,您可以访问类,而无需担心包含代码文件.试试吧: $process = new process(); 知道了这一点,有几种方法可以使用template_functions类 只需使用它: 如果您创建了它的实例,则可以在代码的任何部分访问该类: class process
{
//all process with system and db
public function doSomethging() {
// create instance and use it
$tplFunctions = new template_functions();
$tplFunctions->doSomethingElse();
}
}
实例成员: 以流程类为例.为了使process_functions在流程类中可用,你创建一个实例成员并在某个地方初始化它,在你需要的地方,构造函数似乎是一个好地方: //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();
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
