Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:42:48+00:00 2026-05-27T11:42:48+00:00

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

  • 0

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

   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:42:48+00:00Added an answer on May 27, 2026 at 11:42 am

    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+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this combination possible at all? If yes, will I still need to buy
I have this PHP class class myclass{ function name($val){ echo this is name method
I'm not sure if this is possible at all in PHP but this is
Is a translation of the below code at all possible using PHP? The code
is this possible to do , or do i have to do it all
Is this possible? I want to have the To:, Body, and an Attachment all
I'm curious if this is possible at all since a select * from html
First of all, is this possible? If so: What challenges would I encounter in
I need to generate Entities/Object from selected tables - not all. Is this possible
First of all I'm not sure this is even possible, however I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.