2012-09-06 68 views
0

我需要一些幫助一個檸檬水的PHP代碼是在https://github.com/sofadesign/limonadePHP函數包括

我遇到的問題是,當我嘗試運行

class syscore { 

    public function hello(){ 
     set('post_url', params(0)); 
     include("./templates/{$this->temp}/fullwidth.tpl"); 
     return render('fullwidth'); 

    } 

} 

,然後加載fullwidth.tpl並運行功能全角

fullwidth.tpl

<?php 

global $post; 
function fullwidth($vars){ 
    extract($vars); 
    $post = h($post_url); 

} 

    print_r($this->post($post));  

?> 

似乎傳遞$post_url但我不能當我再次嘗試運行print_r($this->post($post))的全角功能,它說裏面把它傳遞給print_r($this->post($post));

但是它無法找到post()功能

我已經嘗試了數事情像下面

function fullwidth($vars){ 
     extract($vars); 
     $post = h($post_url); 
print_r(post($post)); 

}

試圖重新通過

連接到syscore
$redi = new syscore(); 
$redi->connection() <-- this works 
$redi->post($post) <-- this does not 

這裏是我的滿級syscore

class syscore { 

    // connect 
    public function connect($siteDBUserName,$siteDBPass,$siteDBURL,$siteDBPort, $siteDB,$siteTemp){ 
     for ($i=0; $i<1000; $i++) { 
     $m = new Mongo("mongodb://{$siteDBUserName}:{$siteDBPass}@{$siteDBURL}:{$siteDBPort}", array("persist" => "x", "db"=>$siteDB)); 
     } 

     // select a database 
     $this->db = $m->$siteDB; 
     $this->temp = $siteTemp; 
    } 

    public function hello(){ 
     set('post_url', params(0)); 
     include("./templates/{$this->temp}/fullwidth.tpl"); 
     return render('fullwidth'); 

    } 

    public function menu($data) 
    { 

     $this->data = $data; 
     $collection = $this->db->redi_link; 
     // find everything in the collection 
     //print $PASSWORD; 
     $cursor = $collection->find(array("link_active"=> "1")); 

     if ($cursor->count() > 0) 
     { 
      $fetchmenu = array(); 
      // iterate through the results 
      while($cursor->hasNext()) { 
       $fetchmenu[] = ($cursor->getNext()); 
      } 
      return $fetchmenu; 
     } 
     else 
     { 
      var_dump($this->db->lastError()); 
     } 
    } 

    public function post($data) 
    { 

     $this->data = $data; 
     $collection = $this->db->redi_posts; 
     // find everything in the collection 
     //print $PASSWORD; 
     $cursor = $collection->find(array("post_link"=> $data)); 

     if ($cursor->count() > 0) 
     { 
      $posts = array(); 
      // iterate through the results 
      while($cursor->hasNext()) { 
       $posts[] = ($cursor->getNext()); 
      } 
      return $posts; 
     } 
     else 
     { 
      var_dump($this->db->lastError()); 
     } 
    } 

} 

回答

0

它看起來像您有一些問題的認識,努力使你的模板時PHP正在採取的執行路徑。讓我們更深入地看看,我們?

// We're going to call "syscore::hello" which will include a template and try to render it 
public function hello(){ 
    set('post_url', params(0)); // set locals for template 
    include("./templates/{$this->temp}/fullwidth.tpl"); // include the template 
    return render('fullwidth'); // Call fullwidth(array('post_url' => 'http://example.com/path')) 
} 

}

訣竅解決這一個是瞭解PHP是如何工作包括。當你調用include("./templates/{$this->temp}/fullwidth.tpl")一些你的代碼是在syscore對象的範圍執行,即:

global $post; 

print_r($this->post($post)); 

fullwidth在這一點上,全球範圍內被創建,但還沒有被稱爲。當render調用fullwidth時,您不再處於syscore範圍內,這就是爲什麼您不能將$this->post($post)置於內部而不會觸發錯誤的原因。

好的,那我們該如何解決呢?很高興你問。

  1. 我們或許可以重構syscore::post是一個靜態方法,但隨後這將需要syscore::db是靜態的,並且始終返回相同的MongoDB實例(單例模式)。您絕對不想爲每個syscore實例創建1000 Mongo實例。

  2. 我們可以濫用這個框架。一個更糟糕的解決方案,但它會完成工作。

fullwidth.tpl

<?php 

function fullwidth($vars){ 
    $post_url = ''; // put the variables you expect into the symbol table 
    extract($vars, EXTR_IF_EXISTS); // set EXTR_IF_EXISTS so you control what is added. 
    $syscore_inst = new syscore; 
    $post = h($post_url); 
    print_r($syscore->post($post)); // I think a puppy just died.  
} 

看第二種方式是一個完整的黑客,像這寫代碼很可能意味着你不會得到提升。但它應該工作。

但是讓我們假設你想升職,你會做出好的,閃亮的代碼。

//注意:大寫的類名稱 class Syscore {0}保護靜態$ _db;

public static function db() { 
    if (! static::$_db) { 
     static::$_db = new Mongo(...); 
    } 
    return static::$_db; 
} 

// @FIXME rename to something more useful like "find_posts_with_link" 
public static post($url) { 
    $collection = static::db()->redi_posts; 
    // find everything in the collection 
    $cursor = $collection->find(array("post_link"=> $url)); 

    // Changed to a try-catch, since we shouldn't presume an empty find is 
    // an error. 
    try { 
     $posts = array(); 
     // iterate through the results 
     while($cursor->hasNext()) { 
      $posts[] = ($cursor->getNext()); 
     } 
     return $posts; 
    } catch (Exception $e) { 
     var_dump($this->db->lastError()); 
    } 
} 

然後在你的全角功能,我們沒有做任何處理的實例方法的那個愚蠢的廢話一樣它是一個靜態方法。

function fullwidth($vars){ 
    $post_url = ''; // put the variables you expect into the symbol table 
    extract($vars, EXTR_IF_EXISTS); // set EXTR_IF_EXISTS so you control what is added. 
    $post = h($post_url); 
    print_r(Syscore::post($post)); // static method. \O/ Rainbows and unicorns. 
} 
+0

你搖滾!我不知道爲什麼,但你的回答是直截了當的,寫得很好。它做我所需要的,並幫助我在未來做相同的事情。謝謝 – RussellHarrower