2011-05-25 44 views
0

我有一個類已經加載我的頁面__construct()裏面,之後,我想再次加載這個類,在另一個函數內。這是我的代碼。如何在PHP中的函數內加載一個類?

<?php 

class MainClass { 
    public $config; 

    public function __construct($config) { 
    $this->config = $config; 
    } 

    public function routes() { 
    // here I need to load the config class 
    } 
} 
?> 

內部路由功能,我想打電話給陣列形式配置類文件

+0

我添加了一個缺少'}'你的類定義的結束;請仔細檢查您的原始源代碼是否也缺少關閉'}'。 – meagar 2011-05-25 13:06:12

回答

1

$config已經加載的對象的屬性,因爲你在構造函數中通過了它。如果您需要從另一個成員函數訪問$config,只需使用$this->config->whatever

public function routes() { 
    //here i need to load the config class 
    $something = $this->config->somearray[0]; 
} 
0
public function routes() { 
    require_once "/path/to/YourClass.php" 
    $your_class_instance = new YourClass(); 
} 
相關問題