What does the PHP "new" actually do when instantiating a class?

Asked

Viewed 229 times

1

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

I use the class methods as follows: Setup::$getMetodoX so far so good. However to facilitate its use and not have to include the class in several pages, I make a include of this class in my HEAD.

But looking at the 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 the new of PHP.

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

So I’d like to understand if this new PHP has some other function besides instantiating a class?

  • 1

    You’ve seen what errors/warnings PHP gives. It’s that not appearing style can indicate an error in PHP. Have you seen the browser console if css is being loaded?

  • No error occurs, neither Warning in PHP. but when I comment on the quoted line all js and css files generate error in loading. My page loads normal but without style. But without PHP error messages, it is difficult to understand what might be happening.

  • It is because you must have something that indicates the path of the files in that class. You have already checked what you have in the constructor?

2 answers

5

It has no other function, it is used to indicate that you must create an object and call the builder. I mean, you’ll call the __construct().

In a static class it should not have one, but if it has one, it will be executed when trying to instantiate and there can do any crazy. If it is a framework may be creating a number of global settings, which is crazy. Frameworks usually do crazy things. So the variable wouldn’t need to be used anywhere, it doesn’t even need to exist. Even if I had to do this crazy thing, it should be through a static method and not the constructor.

The specific problem only you can assess, but the functioning of the mechanism is this.

Assess if nothing else has changed that caused the problem.

  • 1

    But this framework is for crazy even in :P

  • I’m not using framework. I’m still doing it the most manual way msm! :/

  • So there’s a huge amount of what you’ve done. But if you did and you don’t know why this is happening, it’s harder for people who don’t know anything about it.

  • Someone else made the Setup class and I was implementing new classes when I came across that. But my failure was not to pay attention to the msm class builder! Anyway, thanks for the help! :)

2


In PHP classes have no type definition or visibility.

For example, there is no such thing:

static class Foo{

}

This is only in the methods and properties.

class Foo
{
    puclic static $bar;

    public static QualquerCoisa()
    {


    }

}

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

$c = new Foo();

In this case, as per @Maniero’s reply, the constructor method is invoked automatically, if available.

Probably this class of yours Setup() has a construction method public function __construct() where operations essential to the operation of the other methods are initiated. Therefore (probably) it results in several failures.

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

Setup::__constructor();

See the documentation for the system you are working with.

  • Ours was exactly that. There was no attack on the builder. Inside it I have a method that arrow some initial settings and so the file loading error occurred. Thanks! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.