繁体   English   中英

类函数内部的PHP函数包括

[英]PHP function inside a class function include

我正在使用Lemonade-php。 我的代码在https://github.com/sofadesign/limonade

我遇到的问题是尝试跑步时

class syscore {

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

    }

}

然后加载fullwidth.tpl并运行功能fullwidth

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());
        }
    }

}

您似乎在理解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为静态方法,并始终返回SAME 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.    
}

第二种方法是完全破解,编写这样的代码可能意味着您不会被提升。 但它应该起作用。

但是,假设您想得到晋升,您将编写出色的代码。

// Note: Capitalized class name
class Syscore {
protected static $_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.   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM