2

抱歉,如果我解释得不好并且没有意义,我有点困惑。

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

 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包含执行包含的类的属性和方法,这完全破坏了包含中的内容,即:

myClass::getSomethingFromDatabase();

我知道它破坏了 myClass 是数据库类的扩展,它调用自身$this来做很多事情,而 $this 现在是对包含该文件的不同类的引用。

我通过实例化我想在包含中使用的对象暂时解决了这个问题,这需要数据库凭据:

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

我不得不define将数据库凭据作为常量来快速轻松地获取它们,我想避免这样做。我是否错过了一种更好的方法来使静态呼叫正常工作?

4

1 回答 1

0

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

正如您在以下示例中所看到的,anonymous_include() 似乎可以满足您的需求。

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

测试.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

包括.php:

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

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

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

包括_2.php:

<?php
if (isset($this)) {
  echo "\$this exists\n";
} else {
  echo "\$this does not exists\n";
}
于 2012-07-12T18:19:42.873 回答