构造中的对象集的变量在destruct中超出范围

时间:2015-12-31 09:46:27

标签: php oop scope

所以,我开始编写一个用于学习目的的微框架的引导脚本(都学习更多关于编程,php和oop)并且我遇到了这种奇怪的意外行为。

启动Config()类的新对象的变量$ config在Bootstrap的__construct中被调用,它是公共的,然后它被用在Bootstrap的__destruct中,它也是公共的。 $ config的变量本身是公共的,并在__construct之前声明,如下所示。

现在,奇怪的是,我在__destruct中使用$ config时收到通知和致命错误,它表示该变量不存在且致命错误是在非对象上调用成员函数(因为$ config doesn'存在)

这是脚本,希望有人可以指出为什么会发生这种奇怪的行为,有可能我错过了某些内容并且行为很有意义但很好,我很想念它然后请指出来。

<?php
basename($_SERVER["PHP_SELF"]) == "bootstrap.php" ? die("No direct script access allowed") : '';


class Bootstrap
{


		public $config;

		protected $start_route;



		public function __construct()
		{
			$this->settings();
			$config = new Config();
			$this->database();
			$this->routing();
			$start_route = new Route($config);
			$start_route->initiate();
		}


		public function run()
		{

			$db_info = new DatabaseInfo();

			$database = new Database([
				'database_type'    => $db_info->get_db('dbdriver'),
				'database_name'    => $db_info->get_db('database'),
				'server'           => $db_info->get_db('hostname'),
				'username'         => $db_info->get_db('username'),
				'password'         => $db_info->get_db('password'),
				'charset'          => $db_info->get_db('dbcharset'),

				'port'             => $db_info->get_db('port'),
				'prefix'           => $db_info->get_db('dbprefix'),
 
				// driver_option for connection, read more from
				// http://www.php.net/manual/en/pdo.setattribute.php
				'option'           => [
					PDO::ATTR_CASE => PDO::CASE_NATURAL
				]
			]);

			/*
			 *
			 * Read is much faster than Write,
			 * while INSERT will increase load time by ~40ms per each query,
			 * COUNT will only increase by ~2ms
			 *
			 */
			$count = $database->count('site_log', [
				'log_type' => 1
			]);
/*
			$database->insert('site_log', [
				'remote_addr' => '127.0.0.1',
				'request_uri' => '/',
				'log_type'    => 1,
				'message'     => 'Test Query'
			]);
*/
			echo 'The are ' . $count . ' rows in log with severity level of 1!<br />';

		}


		// Since it's being called from __constrcut, it will run before run()
		private function settings()
		{

			require BOOTPATH.'core\\config'.CLASSFIX.EXT;

		}


		// Since it's being called from __constrcut, it will run before run()
		private function database()
		{

			require BOOTPATH.'core\\database'.CLASSFIX.EXT;
			return;

		}


		// Since it's being called from __constrcut, it will run before run()
		private function routing()
		{

			require BOOTPATH.'core\\router'.CLASSFIX.EXT;
			return;

		}


		// Since it's being outputed within __destrcut, it will run after run() and basically last
		public function __destruct()
		{

			$script_end = (float) array_sum( explode( ' ',microtime() ) );

			echo 'Welcome to ' . $config->get_conf('site_name') . '<br />';
			echo 'Processing time: '. sprintf( '%.4f', ( $script_end - APP_START ) ).' seconds';

		}


}

1 个答案:

答案 0 :(得分:2)

执行此操作时:

$config = new Config();

您正在构造函数的范围内创建新的$config对象,而不是填充$config类的Bootstrap属性

你必须使用$this->config;访问类属性