PHP 函数引用另一个函数

发布于 2024-12-21 00:05:48 字数 2122 浏览 3 评论 0原文

在 PHP 中是否可以做到这一点:(已解决:是的,可以做到这一点。)

/// Unit test for class some below
class some_test{
  function test_foo1 () {

      $var->fileName = "name";
      $var->fileLocation = "Location";
      $var->fileType = "png"; 
      $var->someFunction() = foo2();
      // Do $var->someFunction = foo2();

    }

    function foo2() {
      ....do something ....
    }
}

// Class Under test
class some {
      function foo1(){
        $var = getObjWith_someFunction();
        if(isset($var))
           $var->someFunction();
      }
    }

我需要这样做,因为当我为调用 someFunction() 的类编写此单元测试时,它说someFunction() 不存在。所以我需要在测试用例中创建一个虚拟函数。

  Code example:

   class pictureManager {

    public function getPicture() {
       try {
         $picObj = getPicObj(1); 
       }
       catch (Exception) {
         if (isset($picObj)) $picObj->showMessage();
       }
     }

     public function getPicObj($id){ // Need to mock this function to return picClass object
       return new picClass($id);
     }
   }

   class picClass {
     public $id;
     public function __construct($id){
       $this->id = id;
     }

     public function showMessage(){
       echo "in this function";
     }
   }

   Unit Test for class pictureManager:

   class pictureManager_test extends PHPUnit_Framework_TestCase {

     public function test_getPicture() {

        $fixture = $this->getMock('pictureManager', array('getPicObj'));
        $arg = 1;

        // Here I am creating the return obj on the fly. Can this be done? (Yes)
        $returnValue->id = 1; 
>>>     $returnValue->showMessage() = someThing();  // This is where I am stuck

//Solution: $returnValue->showMessage = someThing();   
//Remove the brackets (). Since I cant answer my question I am writing it here.

        $fixture::staticExpects($this->once())
                           ->method('getPicObj')
                           ->with($arg)
                           ->will($this->returnValue($returnValue));
        $fixture->getPicture(); 
     }

   }

Is it at all possible to do this in PHP: (Solved: Yes it is possible to do this.)

/// Unit test for class some below
class some_test{
  function test_foo1 () {

      $var->fileName = "name";
      $var->fileLocation = "Location";
      $var->fileType = "png"; 
      $var->someFunction() = foo2();
      // Do $var->someFunction = foo2();

    }

    function foo2() {
      ....do something ....
    }
}

// Class Under test
class some {
      function foo1(){
        $var = getObjWith_someFunction();
        if(isset($var))
           $var->someFunction();
      }
    }

I need to do this because when I am writing this unit test for a class where someFunction() is being called, it says that the someFunction() does not exist. So I need to create a dummy function in the test case.

  Code example:

   class pictureManager {

    public function getPicture() {
       try {
         $picObj = getPicObj(1); 
       }
       catch (Exception) {
         if (isset($picObj)) $picObj->showMessage();
       }
     }

     public function getPicObj($id){ // Need to mock this function to return picClass object
       return new picClass($id);
     }
   }

   class picClass {
     public $id;
     public function __construct($id){
       $this->id = id;
     }

     public function showMessage(){
       echo "in this function";
     }
   }

   Unit Test for class pictureManager:

   class pictureManager_test extends PHPUnit_Framework_TestCase {

     public function test_getPicture() {

        $fixture = $this->getMock('pictureManager', array('getPicObj'));
        $arg = 1;

        // Here I am creating the return obj on the fly. Can this be done? (Yes)
        $returnValue->id = 1; 
>>>     $returnValue->showMessage() = someThing();  // This is where I am stuck

//Solution: $returnValue->showMessage = someThing();   
//Remove the brackets (). Since I cant answer my question I am writing it here.

        $fixture::staticExpects($this->once())
                           ->method('getPicObj')
                           ->with($arg)
                           ->will($this->returnValue($returnValue));
        $fixture->getPicture(); 
     }

   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2024-12-28 00:05:49

PHP 5.3 是第一个接受这一点的版本,所以是的可能的。

但如果您使用的是任何早期版本,则需要使用 create_function() 函数。

5.3 还允许以类似于 lambda 函数的方式动态创建函数。

总结如下:

// Obvious
function a() {
  // do something
}

$b = a; // this works in PHP 5.3+

$c = create_function('$someArgs', 'return $someArgs;'); // PHP 4.0.1+

$d = function() { /* do something else */ }; // PHP 5.3+

PHP 5.3 is the first version to accept this, so yes it is possible.

But if you are on any previous version, you will need to use the create_function() function.

5.3 also allows dynamic creation of functions in a way similar to lambda functions.

Here's a sum-up:

// Obvious
function a() {
  // do something
}

$b = a; // this works in PHP 5.3+

$c = create_function('$someArgs', 'return $someArgs;'); // PHP 4.0.1+

$d = function() { /* do something else */ }; // PHP 5.3+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文