Is calling methods in parent class a bad idea? - php

In real world project, we had a pretty long PHP class, and during refactoring it was broken into 2 smaller classes, ClassA and ClassB, where ClassA extends ClassB (just an example names). There were some issues with legacy code, so we had to do that way.
In ClassB we call some methods from ClassA (they're not overriden, I mean, methods doesn't exists in ClassB). Is that a bad idea in OOP?
Edit: obviosly, in the rest of code we always initiate only ClassA. But wondering is calling parent class from the child a big "NO" in OOP terms?

What you're describing is a common pattern in OOP. The parent class should be an abstract class, so it can't be instantiated by itself, only the child classes can be instantiated.
The child classes are all required to implement methods that aren't in the parent class, but some methods are common to all the classes, and they're implemented in the parent. These common methods then defer to the children for some implementation aspects that are child-specific.

Related

Scenarios for using Abstract Class and Interface Class

I know the concepts of Abstract Class and Interface Class.
But I want real time example which explains both the concept in our design.
Can anyone help with this?
Both have little different scenerios...
Abstract classes also contains method definition as you know interface does not. But if you defines abstract class you can not inherit more than one classes from child class. But In case of interface you can do that and implement the methods declared inside the interface.
These scenerios are used in the software development as we just declared the signature of the method and then user can write his.her own code inside the method by overriding it.
Here I am mentioning some point what we can do with abstract class and interface
In abstract class you can define abstract methods which should be public or protected. but in Interface you can define public abstract method only.
In abstract class you can define data members and constants but in interface you can define constants only.
In abstract class you can define body of method and you can inherit in sub class, but in interface you can not define body of method.
you can implement multiple interface in a sub class, but you can not inherit multiple abstract classes in a sub class.
Example
In Major Frameworks, uses interfaces for different types of data source class. If we want to make data source class for new introduced database then we must have to implement their interface in our class so it make well maintained code and methods parameters.

How can an abstract class be executed if it cannot be instantiated

I was trying out some OOP programming in PHP and when I instantiated a class, it generated an error saying that that class needs to be abstract.
When does a class need to be abstract? And how can abstract classes be called? Thanks
I can't think of a situation where you would get an error that a class needs to be abstract. Its more likely that you got an error when trying to instantiate an abstract class. Something along the lines of "Cannot create instance of an abstract class".
Abstract classes cannot not be instantiated directly. Instead they are extended by a child class (a concrete class) that can be instantiated. So an abstract class is like a template that provides some common functionality for other sub-classes. Usually it includes some abstract methods that the child classes must implement.

In which case we need to use abstract class and interface in php

Abstract class abc{
---
}
I am new to OOP.I want to lean the object oriented programming concept. There is a confusion between abstract classes and interfaces. Where do we have to use this and which condition we have to you abstract and interface class. Please suggest me tutorial links so that i can easily get the functionality. Suppose we are going to drink water because i need it. This is the situation when i need water so i take water. In same case abstract and interface is needed.
An abstract class is a special kind of class that cannot be
instantiated. So the question is why we need a class that cannot be
instantiated? An abstract class is only to be sub-classed (inherited
from).
In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
An interface is not a class. It is an entity that is defined by the
word Interface. An interface has no implementation; it only has the
signature or in other words, just the definition of the methods
without the body.
As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
Read more - http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

What is the difference of abstract class and non-abstract class when inheriting

I have searched for hours of the advantages of using a abstract class over a non-abstract class in php. I know that the abstract class can not be instantiated which is a good feature of singleton design pattern. But my point is, since they both can serve as a base class, is there any other reason to use an abstract class? All of the answers like this one What are the advantage if i use abstract class in php? I found didn't mention this, they just answered another question, namely, what is the advantage of using a base class?
So, my question is that is there any reason to use an abstract base class other than a normal base class except for that it cannot be instantiated?
An abstract class cannot be directly instantiated, but it can contain both abstract and non-abstract methods.
If you extend an abstract class, you have to either implement all its abstract functions, or make the subclass abstract.
You cannot override a regular method and make it abstract, but you must (eventually) override all abstract methods and make them non-abstract.
Abstract keywords are used to label classes or methods as patterns. It's similar to interfaces but can contain variables and implementations of methods.
There are a lot of misunderstandings concerning abstract classes. Here is an example of an abstract Dog class. If a developer wants to create some basic Dog class for other developers or for himself to extend he declares the class as abstract. You can't instantiate the Dog class directly (nobody can), but you can extend Dog by your own class. SmartDog extends Dog etc.
All methods that are declared to be abstract by the Dog class must be implemented manually in each class that extends Dog.
For example, the abstract class Dog has an abstract method Dog::Bark(). But all Dogs bark differently. So in each Dog-subclasses you must describe HOW that dog barks concretely, so you must define eg SmartDog::Bark().

Static classes inheriting from abstract class in PHP

I have stumbled upon an interesting problem/bug that I eventually solved, but the solution is not what I expected or would like it to be.
The setup is simple. There is an abstract static class Factory, which has a reference to a singleton (Registry) as well as two static properties, model and table.
There are a number of static classes building upon/inheriting from this abstract class and they all have an init() method in which they set the model and table properties. Of course, since model and table are static they can only have one value which is the same for all the child classes of the abstract Factory class. This is the problem/bug.
However, my aim is to have each child class have its own model and table so I am forced to declare model and table in each child class as a static property. This seems a bit cumbersome (and not very DRY), but it seems to me that this is the only solution if I want to have (1) the classes inherit from the abstract Factory class and (2) remain static.
Is my assumption correct or is there another approach that I am missing?
The goal is to have different values for $model and $table in different static child classes?
Assuming that is the case, I don't see how You could possibly accomplish that without explicitly defining them different in each child class.
I don't think it is cumbersome. I see it as a good practice - by keeping functionality where it belongs.

Categories