Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between using normal functions and class methods in PHP?

Tags:

oop

php

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.

like image 666
Sandro Antonucci Avatar asked Aug 22 '10 02:08

Sandro Antonucci


People also ask

What is the difference between function and class in PHP?

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.

What is difference between function and method in PHP?

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.

What is the difference between function and methods?

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.

Why we use classes instead of functions?

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.

What is the difference between static function and normal function in PHP?

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.


1 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 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 Dogs 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?

Wikipedia:

Object-oriented programming, Encapsulation, Inheritance, Polymorphism

like image 81
NullUserException Avatar answered Sep 22 '22 12:09

NullUserException