Is it possible to use the last method of a static class?

I need a class to inherit from another class. I have a static function _init()

in a base class and I don't want this method to be inherited in a derived class. I tried the keyword final

but it doesn't work. What should I do?

+2


source to share


6 answers


You don't understand what the final means.

One of the fundamental principles of OOP is that if a class is a subclass of another class, then that class receives all the functionality of the class from which it inherits. You cannot inherit only some of the functions of the superclass, all or nothing.

The reason for this is a little tricky to deal with, but once you do, you know it makes sense. Namely, if a piece of code is capable of handling elements of a certain class, it must also handle any subclass of the element. If the subclass did not have all of the functionality of its superclass, you might not be able to.

For example, imagine you have a class that defines how to interact with a payment gateway. Since all payment gateways are different, you cannot (or at least shouldn't) implement a single class that can interoperate with any available payment gateway. But overall, payment gateways work the same way. A request is sent including the user's credit card details and the amount you wish to debit from the card. The payment gateway responds with a message whether the payment is allowed or not.

You implement this functionality with an abstract superclass that defines how you generally interact with payment gateways, then you will create a subclass for every payment gateway you want to use (paypal, sagepay, whatever).



Since these subclasses all inherit from the same base class, you can be sure that they all implement certain methods, and you can send messages to those methods without worry. As long as the payment class you are dealing with is a subclass of your abstract payment class, then everything should work.

If one of the subclasses was unable to implement something from the abstract superclass, then you could not be sure that sending a specific message to the subclass would work or throw an exception. This would make inheritance pretty pointless.

Final in PHP (and indeed most OOP languages ​​implementing it or similar concepts) means "This is the final implementation of this method. If a programmer subclasses me, then he cannot replace my final methods with his own implementation", This does not mean that the methods will not be available in the subclass, you simply cannot change them.

If you really need to remove functionality from a subclass, you can do so by overriding the superclass's method and replacing it with an empty method (the one that does nothing). But do not do this, as it will cause problems of the type described above.

Typically, if you run into situations where you have functionality in a superclass that you don't need in a subclass, it's a code smell that tells you that you might need to rethink your design.

+7


source


You are doing it wrong.

Just the fact that you want the extended class not to have a method means that you are violating the Liskov Substitution Principle .. if reading is difficult: here is an image explaining it.

If you have a situation where you need to do some "work" on an object before you release it into your application, you should use Factory / Builder for it.



class FooBuilder
{
   public function create( $external_condition )
   {
      $object = new Foo;
      if ( $external_condition->has_happened() )
      {
         $object->set_condition( $external_condition );
      }else{
         $object->do_something_else();
      }
      return $object;
   }
}

      

Now you always make instances of the class Foo

through FooBuilder

, declaration when you extend Foo

, it doesn't have any useless methods.

Also, I would say that static methods in classes are a sign of procedural programming. Static variables are nothing more than namespaced global variables masquerading as a class.

+3


source


Static means one for each class, not one for each object, no matter how many instances of the class may exist. This means that you can use them without creating an instance of the class. Static methods are implicitly final because overriding is done based on the type of the object, and static methods are bound to the class, not the object. A static method in a superclass can be overshadowed by another static method in a subclass if the original method was not declared final. However, you cannot override a static method with a non-static method. In other words, you cannot change a static method to an instance method in a subclass.

The final class cannot be extended, i.e. the final class cannot be subclassed. The final method cannot be overridden when its class is inherited. You cannot change the value of the final variable (it is constant).

+3


source


In POO, if class B inherits another class A, then class B will contain all methods from class A, this is the core of the POO legacy. So, I don't want that method to be inherited in the derived class

doesn't make sense here. You must look at a different mechanism to do this.

Also, this is the case most

, a situation like this (where you don't want the derived class to inherit the method) is a sign that your derived class shouldn't inherit at all and where you should be using a different system.

+2


source


Check out this example:

class foo
{
  private static $ stuff = array ('key1' => 1, 'key2' => 2);

  public final static function getstuff ()
  {
   return self :: $ stuff;
  }
}

+1


source


I would agree with pomeh on OOP in PHP, and I would also like to add that the FINAL keyword when used with a method actually means the method cannot be overridden. However, the method will be inherited.

+1


source







All Articles