How to Check in PHP That I'm in a Static Context (Or Not)

How do I check in PHP that I'm in a static context (or not)?

Try the following:

class Foo {
function bar() {
$static = !(isset($this) && $this instanceof self);
}
}

Source: seancoates.com via Google

How to tell whether I'm static or an object?

Admittedly not offhand...but Sean Coates has a cool and fairly simple approach to finding this out:

$isStatic = !(isset($this) && get_class($this) == __CLASS__);

Using $this inside a static function fails

This is the correct way

public static function userNameAvailibility()
{
$result = self::getsomthin();
}

Use self:: instead of $this-> for static methods.

See: PHP Static Methods Tutorial for more info :)

Why PHP uses static methods in object context?

To call a static method, you don't use:

$foobar = new FooBar;
$foobar->foo()

You call

FooBar::foo();

The PHP manual says...

Declaring class properties or methods as static makes them accessible
without needing an instantiation of the class. A property declared as
static can not be accessed with an instantiated class object (though a
static method can).

This is why you are able to call the method on an instance, even though that is not what you intended to do.

Whether or not you call a static method statically or on an instance, you cannot access $this in a static method.

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

You can check to see if you are in a static context, although I would question whether this is overkill...

class Foobar
{
public static function foo()
{
$backtrace = debug_backtrace();
if ($backtrace[1]['type'] == '::') {
exit('foo');
}
}
}

One additional note - I believe that the method is always executed in a static context, even if it is called on an instance. I'm happy to be corrected on this if I'm wrong though.

PHP: check if class implements static method

A static function of a class is by definition a method of that class and it's also callable, hence calling method_exists or is_callable on a static method will always return true

For your use case you do not need to actually know if the method is static or not, calling

call_user_func([$instance, 'c']);

Will do what you want, if 'c' is a static method and $instance either an object or a class name, it will be called 'statically'

Similarly, if you're calling

call_user_func([$instance, 'b']);

If 'b' is a non static method and $instance an object, the method will be called on the object 'non statically'

If you really need to know if the method is static or not, then you should use Reflection which is doing excellent in term of performances, against your beliefs

$rm = new \ReflectionMethod($instance_or_class, 'b');
$rm->isStatic();

PHP: get derived class context in static function? i.e. differ between BaseClass::staticBaseFunc() vs DerivedClass::staticBaseFunc()

Let me introduced you to late static binding.

Consider the following code, it's not exactly like yours but it highlight's the issue I believe you are facing.

<?php

class A
{

public static $string = 'I am from class A';

public static function getString()
{
return self::$string;
}
}

class B extends A
{
public static $string = 'I am from class B';
}

B::getString(); // returns 'I am from class A' ???!
?>

To get around this you can use late static binding to use the variable at run time context (rather than at compile time context)

<?php

class A
{

public static $string = 'I am from class A';

public static function getString()
{
return static::$string; // note the change here
}
}

class B extends A
{
public static $string = 'I am from class B';
}

B::getString(); // returns 'I am from class B' and all is well
?>

Far more information than I can give you is available here: https://www.php.net/manual/en/language.oop5.late-static-bindings.php

Calling methods concept in PHP: static call versus object context

Unfortunately not. As long as there is a method with the name you are trying to call, PHP will call it without considering type of call vs the type of method.



Related Topics



Leave a reply



Submit