2015-12-31 70 views
2

因此,我開始爲我正在爲學習目的而開發的微型框架(都瞭解更多關於編程,php和oop)編寫引導腳本,並且我遇到了這種奇怪的意外行爲。構造中的對象變量超出了範圍

啓動Config()類的新對象的變量$ config在Bootstrap的__construct中被調用,它是公共的,然後它被用於Bootstrap的__destruct,它也是公共的。 $ config的變量本身是public的,並在__construct之前聲明,如下所示。

現在有什麼奇怪的是,我在__destruct中使用$ config時得到一個通知和致命錯誤,它表示該變量不存在,致命錯誤是在非對象上調用成員函數(因爲$ config doesn不存在)

這裏是腳本,希望有人能指出爲什麼會發生這種奇怪的行爲,這可能是我錯過了一些東西,行爲有意義但是很好,我錯過了,然後請指出它出。

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

 

 
class Bootstrap 
 
{ 
 

 

 
\t \t public $config; 
 

 
\t \t protected $start_route; 
 

 

 

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

 

 
\t \t public function run() 
 
\t \t { 
 

 
\t \t \t $db_info = new DatabaseInfo(); 
 

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

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

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

 
\t \t } 
 

 

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

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

 
\t \t } 
 

 

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

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

 
\t \t } 
 

 

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

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

 
\t \t } 
 

 

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

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

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

 
\t \t } 
 

 

 
}

回答

2

當你這樣做:

$config = new Config(); 

要創建在構造函數中的作用域的新$config對象,你是不是填充Bootstrap類的$config財產

您必須使用$this->config;訪問類屬性

+0

我不知道爲什麼。 $這 - >設置();調用包含Config類文件的函數。然後$ config被設置爲新的Config();並且應該在引導類中被廣泛使用,因爲它被聲明爲public,並且在構造函數中被分配了一個對象。 – CoreModule

+0

不,變量'$ config'的範圍只在構造函數中,而不是在寬類中。你正在談論'$ this-> config'這是這個類的公共屬性 – Moppo

+0

它的工作原理,謝謝! :) 我想我誤解了一些oop的概念。 – CoreModule