gpt4 book ai didi

php - 从变量实例化新对象

转载 作者:可可西里 更新时间:2023-10-31 22:06:51 25 4
gpt4 key购买 nike

<分区>

我正在使用以下类来自动加载我的所有类。这个类扩展了核心类。

class classAutoloader extends SH_Core {

public function __construct() {
spl_autoload_register(array($this, 'loader'));
}

private function loader($class_name) {
$class_name_plain = strtolower(str_replace("SH_", "", $class_name));
include $class_name_plain . '.php';
}
}

我在核心类的 __construct() 中实例化该类:

public function __construct() {
$autoloader = new classAutoloader();
}

现在我希望能够像这样在加载器类中实例化对象:

private function loader($class_name) {
$class_name_plain = strtolower(str_replace("SH_", "", $class_name));
include $class_name_plain . '.php';
$this->$class_name_plain = new $class_name;
}

但我在调用 $core-template 时遇到以下错误:

require 'includes/classes/core.php';
$core = new SH_Core();

if (isset($_GET['p']) && !empty($_GET['p'])) {
$core->template->loadPage($_GET['p']);
} else {
$core->template->loadPage(FRONTPAGE);
}

错误:

Notice: Undefined property: SH_Core::$template in /home/fabian/domains/fabianpas.nl/public_html/framework/index.php on line 8
Fatal error: Call to a member function loadPage() on a non-object in /home/fabian/domains/fabianpas.nl/public_html/framework/index.php on line 8

它会自动加载类但不会启动对象,因为使用以下代码它可以正常工作:

public function __construct() {
$autoloader = new classAutoloader();

$this->database = new SH_Database();
$this->template = new SH_Template();
$this->session = new SH_Session();
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com