将PHP文件的结果包含在另一个文件中而不会弄乱对象引用?

时间:2012-06-28 23:31:51

标签: php oop

道歉,如果我解释得很严重而且没有意义,我有点困惑。

我希望能够将PHP文件的输出包含在给定位置的另一个中,为此,我使用缓冲区并包含/ require:

 if (file_exists(CUSTOM_PHP_DIR."/".$filename)) {
   ob_start();
   include(CUSTOM_PHP_DIR."/".$filename);
   $html = ob_get_clean();
 }
 echo $html; //where I want the results to go.

问题是,我把上面的内容包含在一个类的方法中;所以当文件包含 - $this包含完成include的类的属性和方法时,它会完全破坏include中的内容,这就是:

myClass::getSomethingFromDatabase();

我知道它破坏了myClass是一个数据库类的扩展,它调用自己为$this来做一堆东西,而现在这是对包含该文件的不同类的引用。 / p>

我通过实例化我要在include中使用的对象来解决这个问题,这需要数据库凭据:

$newObject = new myClass(DB_CREDENTIALS);
$newObject->getSomethingFromDatabase();

我必须define将数据库凭据作为常量来快速轻松地获取它们,我想避免这样做。我错过了一个更好的方法,我可以让静态调用工作吗?

1 个答案:

答案 0 :(得分:0)

我似乎从评论中理解你已经以另一种方式重构了代码,但是你原来问题的答案是在新的函数上下文中包含你的包含,以丢弃范围中的每个变量

正如您在以下示例中所看到的,anonymous_include()似乎可以执行您想要的操作。

$ php test.php
Another class
bar
--
Direct include
$this exists
--
Anonymous include
$this does not exists

test.php的:

<?php                                                                                  
class main                                                                             
{                                                                                      
  public function another_class()                                                      
  {                                                                                    
    $path = dirname(__FILE__).'/include.php';                                          

    if (!file_exists($path)) {                                                         
      die('The file does not exists :(');                                              
    }                                                                                  

    ob_start();                                                                        
    include($path);                                                                    
    $html = ob_get_clean();                                                            
    echo $html;                                                                        
  }                                                                                    

  public function direct_include()                                                     
  {                                                                                    
    $path = dirname(__FILE__).'/include_2.php';                                        

    if (!file_exists($path)) {                                                         
      die('The file does not exists :(');                                              
    }                                                                                  

    ob_start();                                                                        
    include($path);                                                                    
    $html = ob_get_clean();                                                            
    echo $html;                                                                        
  }                                                                                    

  public function anonymous_include()                                                  
  {                                                                                    
    $include = function() {                                                            
      $path = dirname(__FILE__).'/include_2.php';                                      

      if (!file_exists($path)) {                                                       
        die('The file does not exists :(');                                            
      }                                                                                

      ob_start();                                                                      
      include($path);                                                                  
      $html = ob_get_clean();                                                          
      return $html;                                                                    
    };                                                                                 
    echo $include();                                                                   
  }                                                                                    
}                                                                                      

$main = new main();                                                                    

echo "Another class\n";                                                                

$main->another_class(); // $this exists, but things seems to work fine                        

echo "--\nDirect include\n";                                                           

$main->direct_include(); // $this exists                                        

echo "--\nAnonymous include\n";             

$main->anonymous_include(); // $this exists

include.php:

<?php
class sub
{
  protected
    $foo = 'bar';

  public function foobar()
  {
    echo $this->foo."\n";
  }
}

$sub = new sub();
$sub->foobar();

include_2.php:

<?php
if (isset($this)) {
  echo "\$this exists\n";
} else {
  echo "\$this does not exists\n";
}