Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP中所需的相同上下文创建函数?_Php_Oop_Class_Function_This - Fatal编程技术网

如何使用PHP中所需的相同上下文创建函数?

如何使用PHP中所需的相同上下文创建函数?,php,oop,class,function,this,Php,Oop,Class,Function,This,这个问题是由于我试图在文件上使用它时出错造成的。我有一个包含页面类的文件: class Page { function whatHappen() { echo "this may work"; } function helloWorld() { echo "This is my page!"; require( "usethis.php" ); // --> this works simila

这个问题是由于我试图在文件上使用它时出错造成的。我有一个包含页面类的文件:

class Page {
   function whatHappen()
   {
      echo "this may work";
   }  
   function helloWorld()
   {
       echo "This is my page!";
       require( "usethis.php" );      // --> this works
       similar_require( "usethis.php" );    // ---> with this I get an error
   }
   function write()
   {
       $this->helloWorld();
   }
}
以及类似于以下要求的功能:

function similar_require( $filepath )
{
   require( $filepath );
}
在usethis.php文件中,我有以下内容:

<?php
   $this->whatHappen();
?>

如何做类似的工作\u require和require like相同的功能?

也许这样可以:

function similar_require( $filepath, $this )
{
   require( $filepath );
}


在usethis.php中,您试图访问
$this
,它未在
类似\u require
函数的范围内声明。
了解有关可见性的信息:


所有这些代码看起来都很脏:不要在方法或函数中使用“require”或“include”——这就像使用全局变量一样(全局变量是非常糟糕的事情)。

到底是什么错误?您是否同时使用两个require语句?如果是这样的话,试着注释掉第一个require语句。
similar_require( "usethis.php", $this );