Php 通过变量传递类实例
我正在创建一个插件系统,其中插件存储在php类中。每个插件都使用主包的类实例来执行其功能Php 通过变量传递类实例,php,oop,Php,Oop,我正在创建一个插件系统,其中插件存储在php类中。每个插件都使用主包的类实例来执行其功能 <?php class main { //Class methods ... function execute(){ ... } function load_plugin($name) { ... include 'plugins/' . $name . '.php'; $this-> {$name}=new $name ($this);
<?php
class main {
//Class methods
...
function execute(){
...
}
function load_plugin($name) {
...
include 'plugins/' . $name . '.php';
$this-> {$name}=new $name ($this);
return true;
}
在“plugins/example.php:”中
<?php
class example {
function __construct($main){
$this->main=$main;
}
function delete(){
$this->main->execute();
}
}
问题是我无法将类实例传递到插件中。我尝试使用define,但无法管理。
如何将当前类实例传递到插件中?要使用main类的方法,必须扩展示例类
我认为您需要实现uuuu get和uuu set方法,这样您的main->load_uplugin$name及其内部:$this->{$name}=new。。。。甚至会起作用。也可能你会使用扩展而不是将类注入构造函数。这意味着我会为每个插件使用一个新的类实例。我真正想要的是为所有插件使用一个类实例,比如:`$main->plugin1->method$主->插件2->方法2$main->plugin3->method3`
class example extends main{
//you cade
}