Why set variables inside the construct of a PHP class when you can set them when they are declared?

First reason : re-usability Second reason : The credentials for your database connection don't belong in a database class, this should be handled in your application. Your class should only know how to accept and use them - not define them A good example case is having other login information on your development machine then on your staging/live machine This instantly shows you the problem with declaring in the constructor I didn't notice what the attempt was until your comment. I'm not used to see var anymore I guess.

Luckily I wasn't the only one. Of course declaring like that is impossible.

First reason: re-usability. Second reason: The credentials for your database connection don't belong in a database class, this should be handled in your application. Your class should only know how to accept and use them - not define them.

A good example case is having other login information on your development machine then on your staging/live machine. This instantly shows you the problem with declaring in the constructor. I didn't notice what the attempt was until your comment.

I'm not used to see var anymore I guess. Luckily I wasn't the only one. Of course declaring like that is impossible.

Third reason: Class member initialisation only works with constants, not expressions. New mysqli( 'host', 'name', 'pass', 'directory' ); would never work. – hakre Sep 16 at 11:40.

You have an error in your question. This does not work in a class: class foo { var $mysqli = new mysqli( 'host', 'name', 'pass', 'directory' ); } Try the Demo to see what does not work here. So maybe one (!) reason to write class foo { var $mysqli; public function __construct() { $this->mysqli = new mysqli( 'host', 'name', 'pass', 'directory' ); } } is because there is no alternative to it (between the two alternatives you offered in your question only, naturally.

This is horrorful bad practice to do so w/o a delicate design based on dependency injection and other high-level design abstractions - or more precise: This is making mysqli a dependency of foo - including the database configuration). Next to that, please don't use var, use private, protected or public instead.

Downvoter: Please share your thoughts. – hakre Sep 16 at 11:46.

You might find this article on Java object initializers to be of some interest. Although many of the concepts in the article are specific to Java and not relevant to PHP, you might find some of these points interesting: Before you use a property in a class, you generally want it to be initialized to some value (in PHP, this is not a requirement, but in practice I rarely see this guideline violated). In Java subclasses, only the "no-arg" parent constructor is called by default, which means that you can't guarantee that a subclass will call the "correct" constructor.In PHP, the problem is compounded because you can't guarantee that the subclass will call the parent class' constructor at all.

Initializing properties directly in the parent class declaration ensures that those properties always start off initialized, even if the subclass constructor doesn't initialize them.

For ease of access. Declaring them all in one central location allows you to, well see a list of all variables. It's not a big deal if you declare them when it is created if you are on a solo project.

If you are working with teams it makes more sense to follow some standard so you don't get confused.

Although not flexible, setting variables when you declare it may be useful if you plan to use a class without creating an instance for some reason: class MyClass { public $MyVar = 'hello'; function MyFunction() { return self::MyVar; } } echo MyClass::MyFunction().

I would recommend that you set these variables as constants in another file so that you can get them from anywhere and you can reuse the code so that it can adapt to any project. I use build-from-scratch-modules with a file filled with constants UPDATE NOTE: I appreciate the minuses even though all frameworks have a configuration file where information like this is stored...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions