magnetica Posted July 31, 2010 Share Posted July 31, 2010 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 Quote Link to comment Share on other sites More sharing options...
Alex Posted July 31, 2010 Share Posted July 31, 2010 What's wrong with that method? Quote Link to comment Share on other sites More sharing options...
Mchl Posted August 1, 2010 Share Posted August 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 1, 2010 Share Posted August 1, 2010 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... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.