Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call parent static method in php

Tags:

I have a base class A:

class A {
   public static function a() {
      ...
   }
   public static function b() {
      ...
   }
}

and an extended class B

class B extends A {
   public static function a() {
      ...
   }
   public static function c() {
      ...
   }
}

I would like to be able to call all the methods using B:: How would I call A::b, using B::?

like image 899
Banana Avatar asked Sep 05 '13 12:09

Banana


People also ask

How do you call a parent class from a static method?

A static method is the one which you can call without instantiating the class. If you want to call a static method of the superclass, you can call it directly using the class name.

How static method is invoked in PHP?

To add a static method to the class, static keyword is used. They can be invoked directly outside the class by using scope resolution operator (::) as follows: MyClass::test();

How do you call a static method?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.

Can we call static method from child class?

If we call a static method by using the parent class object, the original static method will be called from the parent class. If we call a static method by using the child class object, the static method of the child class will be called.


1 Answers

You should be able to accomplish this as easily as:

class B extends A {
   public static function a() {
      parent::a();
   }
}

See the docs

like image 184
brenjt Avatar answered Sep 19 '22 18:09

brenjt