PHP. OOP. Abstract classes and interfaces

Главная » Tutorials » PHP » PHP. OOP. Abstract classes and interfaces

In today lesson we will discuss abstract classes and interfaces in php and talk about difference between it.

Abstract classes are implements with adding key word abstract. Abstract classes can have properties and methods. But unlike simple classes methods in abstract classes have not body. Key concept of abstract methods is you must implement these methods in children classes.

Interface is abstract class, but interface has not got properties. Interface implements with key word interface.

One more key concept is class can extends only one class, but it cant implements several interfaces.

Difference in using interfaces and abstract classes is very “thin” problem. Abstract class unites family classes. For example, you have Car class. And class Ford, Toyota extends from Car class. They have common methods (declared in abstract class).
But if you have got Bike class, it makes no sense to implement, for example, changeOil() method. For such classes you need to use interfaces.

Подписаться на рассылку

Будьте в курсе - получайте последние статьи на свой email

Ваша подписка успешно оформлена

Pin It on Pinterest

Share This