71942

PHP: When would you need the self:: keyword?

I just asked this question about the self:: keyword and didn't realize the static:: keyword existed.

It seems to me that you could use ClassName:: if you needed to reference a particular class explicitly, and otherwise you'd want self:: to mean the class that called the method. I'm trying to think of an example of when you would need self:: where you couldn't just use ClassName::.

<strong>Could you replace all occurrences of self:: with ClassName::, where ClassName is the name of the class enclosure that its found in?</strong> Not that it'd be better or even equivalent semantically, but more functionally speaking...

Answer1:

self forwards static calls, while ClassName does not, in other words: it resolves to the current class instance.

There may be some cases where you want to reference to the current instance and not an extended instance of the class.

This only really matters for late static binding, seen in PHP 5.3 and up, and why the static keyword was introduced.

Answer2:

I probably describe self:: as to ignore class being overwritten or polymorphic as it will always use the parent class even it has been overwritten.

class A{ public function foo(){ echo "hello Mike"; } } class B extends A{ public function foo(){ echo "this will be ignore"; } } $tryThis = new B(); $tryThis->foo(); // Above will print out // Hello Mike

Answer3:

<strong>Short Answer: Yes</strong>

I was curious if this would be affected by namespace changes, inheritance or protected status on functions, but it is not. I can't think of anything else that could affect it.

I think self is better than ClassName simply because it continues to work if you change the class name on the enclosing class or paste the code into a new class.

Proof:

namespace Foo; Class B{ protected static function printOut() { echo "Defined in Foo\B name: ".__CLASS__."\n"; } } Class A extends B{ public static function testClassName(){ A::printOut(); } public static function testSelf(){ self::printOut(); } public static function testStatic(){ static::printOut(); } } namespace Bar; use Foo\A as B; Class A extends B{ protected static function printOut(){ echo "Defined in Bar\A name: ".__CLASS__."\n"; } } $a = new A(); $b = new B(); $a->testClassname(); //Defined in Foo\B name: Foo\B $a->testSelf(); //Defined in Foo\B name: Foo\B $a->testStatic(); //Defined in Bar\A name: Bar\A $b->testClassname(); //Defined in Foo\B name: Foo\B $b->testSelf(); //Defined in Foo\B name: Foo\B $b->testStatic(); //Defined in Foo\B name: Foo\B

As you can see, the only time Bar\A->printOut() is called is when you use static in Foo\A

Recommend

  • Invoke the editor in the interactive mode
  • Android - Enabling and Disabling Bluetooth - SDK 3 (OS 1.5) - programatically?
  • how do i unzip stream while reading it in c# [closed]
  • WatiN File Upload
  • Using Reactive Extension for certain KeyPress sequences?
  • How to change the scope of an existing binding in Ninject
  • How an included partial insert code into parent's block?
  • Creating a checkmark inside a Pickerview [duplicate]
  • python, confused in decorate and closure
  • DropShadowPanel adapt to button template style
  • Use of multiple threads in a Java program and vs need to create Swing objects on EDT
  • grep: matching on literal “+”
  • What's the usecase for ons.ready()?
  • Duplicate Entries in DB
  • Undefined reference to my own library
  • Fluent NHibernate automap list of strings with nvarchar(max)
  • Express.js : POST data as KEY of a req.body object instead of VALUE of req.body?
  • Difference between two forms of Perl filename wildcard command
  • help('modules') crashing? Not sure how to fix
  • Can XOR be expressed using SKI combinators?
  • Why isn't my “Fizz Buzz” test in R working?
  • crash in __tcf_0
  • How can I mock ui-router's resolve values when testing a state's configuration?
  • For loop with if condition on multiple R functions
  • Content-Length header not returned from Pylons response
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Setting up SourceTree to merge unity3d scenes with UnityYAMLMerge
  • Refering to the class itself from within a class mehod in Objective C
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Calling of Constructors in a Java
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Acquiring multiple attributes from .xml file in c#
  • Memory offsets in inline assembly
  • How to get Windows thread pool to call class member function?
  • Does armcc optimizes non-volatile variables with -O0?
  • reshape alternating columns in less time and using less memory
  • How can I use threading to 'tick' a timer to be accessed by other threads?