2010-08-17 64 views
1
class Theme 
{ 
    function __construct() 
    { 

    } 

    function load($folder, $file) 
    { 
     $theme_path = ROOTPATH . '/theme/' . $folder . '/' . $file . '.php'; 
     require_once($theme_path); 
     return TRUE; 
    } 
} 

上的index.phpPHP新手類

<?php 

require class.theme.php 
$theme = new Theme; 
$theme->load('site','index'); 
?> 

在我的網站/ index.php文件

<?php 
// to work i need another $theme = new theme; why can i do this ? can i make 
it make it work twice or more inside the function load ? 
$theme->load('site','header'); 
$theme->load('site','footer'); 
?> 

不知它需要$主題=新的主題;再次在網站/ index.php

是否有另一種方式使其工作?也許我的班級設計不好,或算法失敗。

編輯*更多信息 好吧,所以我試圖做的是加載頁眉查看頁腳視圖。

+0

我不明白你的問題是什麼或不起作用? – 2010-08-17 12:27:25

回答

0
class Theme 
{ 
    function __construct() 
    { 

    } 

    function load($folder, $file) 
    { 
     $theme_path = ROOTPATH . '/theme/' . $folder . '/' . $file . '.php'; 
     return $theme_path; 
    } 
} 

上的index.php

<?php 
require class.theme.php 
$theme = new Theme; 
require_once $theme->load('site','index'); 
?> 

這做的伎倆一會兒,謝謝你們。

1

對象「$主題」並不在請求「網站/ index.php文件」,從「的index.php」你的對象走了貫穿幾個文件仍然存在,所以......

如果不是這樣,我得到你的問題完全錯誤:)

2

我們不知道你的兩個.php文件之間的關係,所以它很難回答。

如果您將$ theme定義爲新主題,則範圍規則仍然適用:您的定義/實例只在其範圍內有效。您將不會擁有全局主題對象。獨立於任何類別/對象設計。

0

儘量使負載功能公衆:在我的網站/ index.php文件

<?php 
// to work i need another $theme = new theme; why can i do this ? can i make 
it make it work twice or more inside the function load ? 
require_once $theme->load('site','header'); 
require_once $theme->load('site','footer'); 
?> 

class Theme 
{ 
    function __construct() 
    { 

    } 

    public static function load($folder, $file) 
    { 
     $theme_path = ROOTPATH . '/theme/' . $folder . '/' . $file . '.php'; 
     require_once($theme_path); 
     return TRUE; 
    } 
} 
+1

默認情況下,PHP類成員(包括字段和方法)都是公共的。 – 2010-08-17 12:57:27