Check if a function is executing from a static context

I am writing a PHP class and have included a couple of static functions for quick access, as they are common to use and easy to use. However, they use an object in them to access the database. I will most likely use these static methods from both a static and non-static context in all my code so that I can check if 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 that is part of the function that will be used statically). Is there a way to test this in a function so that I can use an instance object if a function is called from a non-static context and creates its 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. } } 
+6
source share
5 answers

When a method is declared as static, not only the $ this magic variable is not available (returns NULL), but it is not possible to determine if the function was actually called from a static context. From the reverse, it follows that for a static method, a call to the $ object-> () method is internally translated to className :: method () at run time.

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

+7
source

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

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

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

+3
source

Bottom line - do not create classes containing only static functions. This is not OOP, it's just your old procedural code disguised as oop

If you execute functions statically, there is no $ this, since there is no object. You will also become the static variable private $db and use it as self::$db .

However, I would recommend abandoning this template and writing something like:

 class Foobar { protected $_connection = null; public function __construct( DBConnect $db ) { $this->_connection = $db; } public function some_function() { $db = $this->_connection; // Do processing with $db, etc. } } 

And you use this class as follows:

 $db = new DBConnection( /* your password , user , host , etc. */); $stuff = new FooBar( $db ); $stuff->some_function(); $blah = new DifferentClass( $db ); 

This way you use the same connection with all classes that require it. And now the FooBar class FooBar not responsible for establishing a connection or should know your connection details.

+1
source

Solution 1: make the $db variable private and use it via getter.

Solution 2: Implement a singleton pattern on the db side.

0
source

Even when calling a static method from an instance (not recommended), you do not have access to $this , so your class will be deadly.

 class sns { public static function moo() { var_dump($this->fng); } public function goo() { var_dump($this); } } sns::moo(); $_ = new sns; $_->moo(); $_->goo(); 

As another poster pointed out from the php manual.

$db however, can be defined statically instead, so you only need to create it regardless of whether the object is an instance or not.

0
source

Source: https://habr.com/ru/post/887018/


All Articles