2016-05-20 70 views
0

的實例變量我有一個類,我通過撥打:訪問在構造

$tagIt_Instance1 = new TagIt; 
$tagIt_Instance1->tag_table = 'tags'; 
// if maintenace is set to true, fill out these (for update) 
$tagIt_Instance1->tag_object_table = 'projects'; // the name of your table 
$tagIt_Instance1->tag_object_fieldname = 'project_tags'; // the name of your field 
$tagIt_Instance1->tag_object_id = isset($_GET['id']) ? $_GET['id'] : ''; // the corresponding id for the row of the table 
// end maintenance 
if (isset($_GET['tag_it']) && $_GET['tag_it'] == true) { 
    $tagIt_Instance1->TagItAjax(
     isset($_GET['action']) ? $_GET['action'] : '', 
     isset($_GET['term']) ? $_GET['term'] : '', 
     isset($_GET['match_id']) ? $_GET['match_id'] : '', 
     isset($_GET['tag_object_id']) ? $_GET['tag_object_id'] : '' 
    ); 
} 

在課堂上,在構造函數中,我想訪問$tag_table

class TagIt 
{ 

    // edit defaults 
    private $debug = true; 
    /* this will reload the page if a new tag cannot be added. suggested to leave true as otherwise the user 
    could submit a page with a tag that is not in the system because of said error. if maintenance is enabled 
    then the tag would be removed eventually when matching occurs, but lets keep the db clean */ 
    private $reloadOnError = true; 
    private $confirmationMsgAdd = 'Do you want to add the new tag'; // no question mark or tag identifier, thats handled by jquery 
    private $confirmationMsgDel = 'Do you want to delete the tag'; 
    private $confirmationMsgDel2 = 'from the database as well'; 
    private $errorNewTag = 'An error occured. Could not process new tag!'; 
    private $generalErrorMsg = 'An error occured.'; 

    // do allow users to delete special cases 
    private $special_cases = array('featured'); 

    private $select2JS = '<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.js"></script>'; 
    private $select2CSS = '<link rel="stylesheet" href="/neou_cms/plugins/select2.css" type="text/css" media="all">'; 

    public $tag_table = ''; 

    /* enabling this will make sure that only tags in the database are 
    kept for a specific row when tagit checks for matches */ 
    public $maintenance = true; 

    // do not edit!!! /////////////////////// 

    // if maintenace is set to true these become applicable (leave blank here) 
    public $tag_object_table = ''; 
    public $tag_object_fieldname = ''; 
    public $tag_object_id = ''; 

    private $location = ''; 

    public function __construct() 
    { 
     // we don't want the query string messing up the AJAX request, thus we remove it 
     $this->location = strtok($_SERVER["REQUEST_URI"], '?'); 

     echo $this->tag_table; 
    } 

然而,每當我嘗試並訪問$this->tag_table,儘管$this->tag_table在腳本後面的函數中工作,我總是會得到一個空變量。我覺得這可能是一個範圍問題,我不確定。

回答

1

您的$this->tag_table在構造函數中是空的,因爲它仍然引用unset/empty變量(public $tag_table = '';)。

哪個是你的代碼明顯:

$tagIt_Instance1 = new TagIt; 
$tagIt_Instance1->tag_table = 'tags'; 

在上面的例子中,你正在構造對象然後只設置了$tag_table

您可以修改您的__construct()有標籤表格傳遞給它,如果你需要:

function __construct($tag_table) { 
    // we don't want the query string messing up the AJAX request, thus we remove it 
    $this->location = strtok($_SERVER["REQUEST_URI"], '?'); 

    // set the tag table, allowing you to access 
    $this->tag_table = $tag_table; 

    echo $this->tag_table; 
} 

這意味着你可以跳過一步:

$tagIt_Instance1 = new TagIt('tags'); 

這是在事實上,常見的做法在實例化類時通過__construct()方法來「定義」像這樣的變量。

+0

該死的男人尷尬我忘記了,它已經有一段時間了,因爲我已經編碼,我一直試圖圍繞我寫的一些東西,而我回頭哈哈。 – Alex

+0

@Alex我們都在那裏之前哈哈!請記住,您可以通過'__construct()'傳遞所有這些變量,並使代碼更易於維護:) – Darren