Jump to content

Ways of setting vars in __construct()


magnetica

Recommended Posts

Hi all

 

If there a better way of setting variables within classes than taking it through the __construct and setting via $this ??

 

e.g.

 

class SectionsConnect {
protected $var;
public function __construct($var){ 
	$this->var= $var;
        }
}

 

Can you not set the variable automatically as it comes in through the __construct?

 

Thanks Magnetica

Link to comment
Share on other sites

Can you not set the variable automatically as it comes in through the __construct?

 

No you can't.

Assumption that all variables passed to the constructor are to be assigned to object's fields is very often wrong.

Link to comment
Share on other sites

automatically in programming means that the programmer wrote code to do it.

 

The whole point of programming is to write the code you need to cause what you want to happen when and where you want it. Expecting a programming language to do something automatically that you want in a particular case means that those people who wanted something different to occur in that case must first undo what the language did before they do what they actually wanted. The cases where php.net caused the core php language to do things automatically for the programmer have all been depreciated and/or turned off by default and/or soon to be removed because they turned out to be bad ideas because they limited the general purpose nature of the programming language.

 

Parameters in function calls, are just that, parameters. What you do with those parameters is up to the programmer to decide to meet the goal of the code he is writing. One person might initialize class variables, another might create a database connection, another might include a specific file, another might create a session variable, another might create a log file...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.