What is the difference between using regular class functions and methods in PHP?

I am trying to learn how to make the best use of OOP in PHP. Keep in mind that even if I were studying the theory of this new "world", I certainly did not think OOP.

What is the difference between using regular, separated functions and putting them in a class as methods?

Say I have a class called "shop."

It has the following 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, can I still call the deleteitems function inside the retrieveitems function correctly? Even if not in class?

Please help me understand what I am missing.

+6
oop php
source share
7 answers

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 of them is independent of each other. If I do $computerShop->removeItem('water') , $hardwareStore should still have water in its inventory . ( see it in action ) The execution of this procedural method is much more random.


Another interesting 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 can call the eat() method, although they are not explicitly declared in their classes - it was inherited from their parent class Animal . They also have a speak() method that does different things. Pretty neat, huh?

Wikipedia:

Object Oriented Programming , Encapsulation , Inheritance , Polymorphism

+11
source share

One of the most interesting features that I like about classes is encapsulation . You can encapsulate various chains of functions into one important function and call them directly. Encapsulation creates an environment in which you can protect member objects and properties from changes outside the class.

Another important part of classes is groups . To be able to group all the functions and attributes associated with a particular object in one, and the ability to create different instances of these classes as objects, is simply magical. Let's say you have a bunch of functions that reference each other and pass roughly the same arguments. This is a good sign that these functions belong to the class, and these arguments must be members of the class.

Since you are using PHP, imagine that you created a class that creates a panel. Now you can use this class and create different but similar layouts, such as the news panel, ad panel, product display panel, creating various objects and providing content.

You could say that this is also possible with functions, but functions cannot provide the scalability and flexibility provided by the class.

Inheritance

This is one of the main features of OOP; you can create parent and child classes at an infinite level. For example, you created a class car . This class has attributes such as wheels, gears, steering, such as drive (), stop (), but you cannot accompany all the cars in this world, so now you will create your child class. In this case, the child classes can be 2WD and 4WD, and again these child classes can also have other child classes with new members and methods. But you can access members and functions up to your parents and great parents.

Polymorphism

This means that you can override a function in parent classes. that is, the simple car control system is different from that of the 4WD car drive system, so you need to redefine the previous parent function and replace it with a new function.

Another is Overloading, its ability to be able to use a function in different cases. For example, for a simple car for driving, it requires starting the engine, mechanism and acceleration, but for modern cars, such as a Ferrari, you only need to start the engine and press the accelerator, you do not need gear, so for these two cases it’s ok to use the drive method () you provide different parameters. And classes make this possible (but this function is not available in PHP :()

+4
source share

Note that OOP accounts for 90% of objects and 10% of classes and interfaces. That is why we call it object oriented programming.

I think your question is too general. There are so many articles about OOP over the Internet (you are not limited to PHP articles - OOP is a paradigm, so you can read articles about Java, etc.)

Object-oriented programming (OOP) is a programming paradigm that uses "objects" - data structures consisting of data fields and methods together with their interactions - to develop applications and computer programs. Programming methods may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance.

Read more about what this quote is entitled.

+3
source share

I like OOP for one reason. Objects Think of a basketball game. Each player is an object. Everyone has a cool person, and each of them has their own set of individual skills and attributes. If I was going to program a basketball game, it would turn into spaghetti code without OOP. With OOP, I can spawn individual people and call them:

Dan is tall and Bill is fast.

 $dan = new Human(Tall->increaseHeight()); $bill = new Human(Fast->increaseSpeed()); 

Without OOP. It would be a mess to gather a bunch of people against each other and track their attributes. I don’t know much about OOP, but organization is one of the reasons why I like it.

+1
source share

Without a class, you still need a structure to hold a set of elements that you add, update, and delete (the receipt will disappear, since you already have the elements).

0
source share

Your question goes at a wider level of PHP, you ask a question about the basic principles of OOP. I would recommend that you still read the basic OOP programming features .

0
source share

The main difference is the region. Here's a great video (even in a computer voice) about the differences: http://www.youtube.com/watch?v=D8jZ0l_GwXQ&feature=related

0
source share

All Articles