I'm trying to learn how to best use OOP in PHP. Please be aware that even if I studied the theory of this new "world" I didn't enter the OOP thinking yet obviously.
What's the difference between using normal, separated functions and putting them in a class as methods?
Let's say I have a class called "shop".
It has these methods: retrieveitems, deleteitems, updateitems, additems
Except for the fact that I can call methods inside methods with a simple "$this", what is the difference between putting them in different functions without a class? I mean, for example, I still can call function deleteitems inside function retrieveitems right? Even if not in a class?
Please help me understand what I'm missing.
One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: 'object'. Class is a programmer-defined data type, which includes local methods and local variables. Class is a collection of objects. Object has properties and behavior.
Method and a function are the same, with different terms. A method is a procedure or function in object-oriented programming. A function is a group of reusable code which can be called anywhere in your program. This eliminates the need for writing the same code again and again.
A function is a set of instructions or procedures to perform a specific task, and a method is a set of instructions that are associated with an object.
By using classes, you're ensuring that methods are only used on one set of data. This adds to the security of the code because you're less likely to use functions where they don't belong.
The difference between static and non static is that you can access a static method without having an instance of the class. $item = getDetail(15); shouldn't work because detDetail is a method in a class and not a function. Save this answer.
OOP provides, among other things, encapsulation.
class Shop {
function __construct($items) {
$this->inventory = $items;
}
function deleteItem($item) {
$key = array_search($item, $this->inventory);
if ($key !== false)
unset($this->inventory[$key]);
}
}
Now you can create instances:
$computerShop = new Shop(array('ram', 'monitor', 'cpu', 'water'));
$hardwareStore = new Shop(array('hammer', 'screwdriver', 'water'));
And each one of them is independent from each other. If I do $computerShop->removeItem('water')
, $hardwareStore
should still have water
in their inventory
. (see it in action) Doing this the procedural way is much messier.
Another cool thing about OOP is that you can use inheritance and polymorphism:
class Animal {
function eat() {
$this->hungry = false;
}
abstract function speak();
}
class Cat extends Animal {
function speak() {
echo 'meow!';
}
}
class Dog extends Animal {
function speak() {
echo 'woof!';
}
}
Now both Cat
and Dog
s can call the method eat()
even though they are not explicitly declared in their classes - it's been inherited from their parent class Animal
. They also have a speak()
method that does different things. Pretty neat, huh?
Object-oriented programming, Encapsulation, Inheritance, Polymorphism
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With