PHP Interface - What is PHP Interface and How to Use It

preview_player
Показать описание
In this video I explain what PHP interface is and how to use it.

Upgrade your Clever Techie learning experience:

Download this video's files here:

``````````````````````````````````````````````````````````````````````````````````````````````




Рекомендации по теме
Комментарии
Автор

Very straightforward. Simple to understand

oyinbrakemimichaelmenebray
Автор

2:41 aren't those supposed to called as "function declarations!?

Ruban_Sn
Автор

I guess I don't understand. Why write the interface if to still need to call the class/functions of the other class? Even the last part, you still copied and pasted the MySQL class to the new Oracle one.

keegers
Автор

"A interface is provided so that you can define a set of functions and then hide the actual implementation of those functions in the implementing class. So, this allows you to change the implementation of those functions without changing how you use it." - Actually, an interface does not hide implementations; it only defines a contract (a set of method signatures) that must be implemented by any class using that interface. Changing an implementation may still impact behavior—it does not guarantee that the new implementation will behave identically. BTW, you are missing public visibility in your methods. Interface methods are implicitly public in PHP. However, when implementing an interface, the methods must also be public; otherwise, you'll get an error. Example:

interface Database {
public function listOrders();
public function addOrder();
public function removeOrder();
}

class MySQLDatabase implements Database {

public function listOrders() {
// list all orders
}

public function addOrder() {
// add an order
}

public function removeOrder() {
// remove an order
}
}


$database = new MySQLDatabase();
foreach ($database->listOrders() as $order){

}


Also, you stated: "Suppose i want to migrate to an oracle database instead of a mysql one. This what the interface is actually for?" This is exactly what an interface is for! The purpose of the interface in this case is to abstract away the database logic so that your code can work with different database systems without changing how you use the Database object. You failed to mention the reason of the Interface and instead just described how to create one. :)

deepwoods-detecting
Автор

still don't get it,
because thats just adding more size to your code while you can define the function + it functionality directly without the interface,
i thought its like in C# where you can update something live in the UI using interface, or at least sharing the same triggered information across the app as in C++ and UE blueprint
but in php it turn to be a joke, it just define a function name for you thats it . 0 real benefits
its like Filling a circle with the same color twice, while the first " Fill " was enough

DoctorMGL
Автор

I don't seem to grasp the concept of, why would you use Interface if we are using the same name methods inside every classes anyway.

ripplesr
Автор

What is your vscode theme setting including font?

ahmedsunil
Автор

When you are creating an object to the actual classes, then why you need an Interface? I mean the interface looks useless here.

NoAdsMurad-tt
Автор

weird, i've never seen anyone use spaces in their class filenames

ellmatic
Автор

I don't understand. Why u are using interfaces.. Thoes work done by change class if your other class has same method you can change it your implementation class

mrmbeautiquethebrand
Автор

You mean Abstraction and Interfaces are same thing

anupkaushik