Are static methods equivalent to functions?

3

In OOP we have the static methods (do not depend on any instance variable). Citing examples:

class Somar {
    public static function soma($a,$b){
        return $a+$b;
    }
}
echo Somar::soma(20,30);

The equivalent of the static method of OOP programming are the normal functions of structured programming?

    function soma($a,$b){
        return $a+$b;
    }

 echo soma(20,30);

That is, the functions of structured programming are statics ?

    
asked by anonymous 18.04.2016 / 15:02

1 answer

2

Yes, functions work like static methods, the difference is that the method is encapsulated in a class which avoids name conflict that is more common in loose functions. Despite the term used, this is not even the true encapsulation we see in OOP.

A function has visibility and global scope equal to the static method (although it may eventually have its visibility limited by another mechanism).

Technically a static method has nothing to do with OOP. In fact the fact of using a class does not mean that it is doing something OOP. To say it's OOP there must be other features in the code.

In fact, the only difference between a static and an instance method is that the instance has a hidden parameter in the syntax called $this and this variable gives access to the object's members. If there was no facility of syntax, an instance method:

function exemplo($p1, $p2) {}

It should be written like this:

function exemplo($this, $p1, $p2) {}

And you'd get the same result, at least from the point of view of instance access.

That is, in fact all methods are functions.

    
18.04.2016 / 15:19