Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if a function is run from a static context

Tags:

oop

php

static

I am writing a PHP class and have included a couple static functions for quick access as they are common for use and are simple in function. However they do use an object in them for database access. I will likely be using these static methods from both a static and non-static context throughout my code so I want to be able to test whether the function was called from a static or non-static context so that I can avoid creating a duplicate object if the function was called from a non-static context (this instance object and the one within the function to be used statically). Is there any way that I can test this in the function so that I can use the instance object if the function is called from a non-static context and create it's own object if the function is called from a static context?

Code Example:

class MyClass {
  private $db;

  function __constuct(){
    $this->db = new DBConnect();
  }

  public static function myFunction(){
    if(/* Is static */){
      $db = new DBConnect();
    } else {
      $db =& $this->db;
    }
    // Do processing with $db, etc.
  }
}
like image 375
Ben Kulbertis Avatar asked Apr 30 '11 16:04

Ben Kulbertis


People also ask

How do you unit test a static function?

All you need to do is wrap the static method call inside an instance method and then use dependency injection to inject an instance of the wrapper class to the class under test.

How do I check if a method is static in Python?

We must explicitly tell Python that it is a static method using the @staticmethod decorator or staticmethod() function. Static methods are defined inside a class, and it is pretty similar to defining a regular function. To declare a static method, use this idiom: class C: @staticmethod def f(arg1, arg2, ...): ...

How do I run a static function?

Calling Static FunctionIt is invoked by using the class name. For example: Math. sqrt(a); //calling the square root function of the Math class.

How do you know if a method should be static?

You should consider making a method static in Java : 1) If a method doesn't modify the state of the object, or not using any instance variables. 2) You want to call the method without creating an instance of that class.


2 Answers

When a method is declared as static, not only is the magic variable $this unavailable (returns NULL), but it is impossible to tell if the function was actually called from a static context. A backtrace implies that for a static method, calling $object->method() is internally translated to className::method() at run time.

http://php.net/manual/en/language.oop5.static.php

like image 152
Shakti Singh Avatar answered Nov 07 '22 05:11

Shakti Singh


You can check for non-static access only if you do not force the method to be static-only. Leave out the static keyword from the function declaration and test with:

public function myFunction(){
    if(!isset($this)) {

(It can still be called as static function, even though you did not declare it as such.)

like image 31
mario Avatar answered Nov 07 '22 06:11

mario