What does "new" PHP do when instantiating a class? [closed]

2

I have a class called Setup with some static methods. But I'm having trouble understanding something that happens.

I use the methods of the class as follows: Setup::$getMetodoX so far so good. But for ease of use and not having to include the class on multiple pages, I make an include of that class in my HEAD .

But when I look at head I noticed the following line of code: $setup = new Setup(); . So I decided to remove this line, since I know that a class of static methods should not receive an instantiation through new of PHP.

But to my surprise. Removing this line my system loses its style (css). However, this $setup variable that instantiates my class is not used anywhere in my project.

So, I would like to understand if this new of PHP has any other function besides instantiating a class?

    
asked by anonymous 29.11.2016 / 13:30

2 answers

2

In PHP the classes have no definition of type or visibility.

Example, something like this does not exist:

static class Foo{

}

This is only in methods and properties.

class Foo
{
    puclic static $bar;

    public static QualquerCoisa()
    {


    }

}

However, even though all methods and properties are static, nothing prevents you from invoking as an instance

$c = new Foo();

In this case, depending on @bigown's response, the constructor method is invoked automatically, if it exists.

Probably this class Setup() has a constructor public function __construct() method where operations that are essential for the operation of other methods are started. So (probably) it results in several faults.

Note that you can not invoke or even declare the constructor method as static:

Setup::__constructor();

See the documentation for the system you are working on.

    
29.11.2016 / 14:29
5

It has no other function, it is used to indicate that you must create an object and call the constructor . That is, it will call __construct() .

In a static class should not have one, but if it has one, it will run when trying to instantiate and there can do any madness. If it is a framework you may be creating a series of global settings, which is crazy. Frameworks usually do craziness. So the variable need not be used anywhere, it does not even need to exist. Even if I had to do this crap, it should be through a static method, not the constructor.

The specific problem only you can evaluate, but the mechanism works like this.

Evaluate if you have not changed anything else that caused the problem.

    
29.11.2016 / 13:39