Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent usage of public parent methods in child instance in PHP?

So let's say I have classes called parent and child, which will be then used from PHP file called caller.php

class Child extends Parent {

}

class Parent {

    public function parentMethod(){

    }
}

caller.php

PREVENTED:

$child = new Child();
$child->parentMethod();

ALLOWED:

$parent = new Parent();
$parent->parentMethod();

I want to prevent calling parentMethod like this. But if I created Parent object I want to be able to call the parentMethod. Is there some way that I can use to hide this method from being public in Child class, but still allowing parent object to call this method publicly?

Only solution I have come up with so far is making those methods protected and then creating an other class that would extend parent and then have public method for each function that it needs, but that doesn't sound very smart.

like image 324
Firze Avatar asked Jul 18 '14 09:07

Firze


People also ask

Can child classes access parent methods?

It has all of the instance variables. The only unusual aspect is that, within child class method definitions, you can't directly access parent class instance variables. For example, if the parent had a height instance variable, child class method definitions wouldn't be able to access this directly.

Can child classes override properties of their parents?

In the same way that the child class can have its own properties and methods, it can override the properties and methods of the parent class. When we override the class's properties and methods, we rewrite a method or property that exists in the parent again in the child, but assign to it a different value or code.

How do you call parent method from child object?

You just have to create an object of the child class and call the function of the parent class using dot(.) operator.

How the child class can access the properties of parent class explain with PHP example?

Inheritance in OOP = When a class derives from another class. The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. An inherited class is defined by using the extends keyword.


1 Answers

Actually, you should ask yourself: why do you need such restriction? You've defined your method as public - thus, you told PHP that it should be visible everywhere. So to prevent child calls you should use private visibility definition.

There is a way to check if call is made from parent class, like:

class ChildClass extends ParentClass {}

class ParentClass 
{

    public function parentMethod()
    {
       if(get_class($this) != __CLASS__)
       {
          throw new LogicException("Somehow due to business logic you're not allowed to call this from childs");
       }
    }
}

But I would not recommend to do that. Reasons are:

  • Readability. Your method is just ordinary public method. Looking to it it's impossible to say either you should use it with child calls or not. Thus, to maintain such code you'll need to check that restriction in code. Now imagine that you have ~50 methods like that. And dozen of classes like that.
  • Possibly, breaking Law of Demeter. Why should parent class be aware of it's childs when using such limitation?
  • Finally, it's just unexpected behavior. Looking to definition, anybody will see that you're extending one class by another. Thus, by definition all inherit methods with proper visibility must be inherited. And your logic changes that.

You may think about composition, not inheritance. That may be right way to implement your logic (however, I can't tell that for sure since I don't know whole background)

like image 84
Alma Do Avatar answered Oct 20 '22 01:10

Alma Do