Monday, September 16, 2019

What exactly is an abstract class in php?




I've been searching around but i cant seem to understand it. Can someone explain what exactly is an abstract class in php?



abstract class theClass {
}


I know the syntax but does it really do?



Answer



Abstract Class – In PHP, these two features of object-oriented programming are used very frequently. Abstract classes that can't be instantiated rather they can be inherited. The class that inherits an abstract class can also be another abstract class. In PHP we can create an abstract class by using the keyword – 'abstract'.



Listing 5 – Sample code for Abstract Class



abstract class testParentAbstract {
public function myWrittenFunction() {
// body of your funciton
}
}


class testChildAbstract extends testParentAbstract {
public function myWrittenFunctioninChild() {
// body of your function
}
}


In the above example, we can create an instance of the child class – testChildAbstract, but we can't create the instance of the parent class – testParentAbstract. As we see that the child class is extending the parent class, we can use the property of the parent class in the child class. We can also implement an abstract method in our child class as per our need.




Source . http://www.phpbuilder.com/articles/application-architecture/object-oriented/advanced-object-oriented-programming-in-php.html


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...