Rommeo Posted September 14, 2009 Share Posted September 14, 2009 I was reading a tutorial about OOP, and there is something makes me confused. Here is the code : <?php class person { public $name; public $age; public function __construct() { $this->name = "mr smith"; $this->age = 0; } public function blabla etc.. } ?> I know what the constructor is for.. But we could code this class like this : <?php class person { public $name = "mr smith"; public $age = 0; public function blabla.. etc. } ?> So my question is :why he assigned the variables like in the first example, we can code it like the second example though. What's the benefit of the first one ? Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/ Share on other sites More sharing options...
AviNahum Posted September 14, 2009 Share Posted September 14, 2009 <?php class person { public $name; public $age; public function __construct($n, $g) { $this->name = $n; $this->age = $g; } } $a = new person("my name" , "my age"); ?> it would nicer to work this way, right? Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-917952 Share on other sites More sharing options...
mikesta707 Posted September 14, 2009 Share Posted September 14, 2009 for your example, yeah it doesn't really matter. However, that is a tutorial, and it really is just showing you the syntax of how constructors go, and not really trying to convince you that you should always assign variables in your constructor. by the way, you can also make a constructor this way class foo{ //below is the constructor public function foo($msg){ echo $msg; } } now if you were to create the function like so $foo = new foo("Hello World!"); it would output Hello World! kind of off topic, but yeah Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-917972 Share on other sites More sharing options...
Rommeo Posted September 14, 2009 Author Share Posted September 14, 2009 I see, so assigning the values in constructor or in decleration part does not make any difference..? Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918442 Share on other sites More sharing options...
RussellReal Posted September 14, 2009 Share Posted September 14, 2009 a constructor is just the function to trigger when the object is instantiated.. whether you specify it in the class or in a method inside the class, it will still do the same thing, however, there is an exception to this.. if you make the variable declared within the class not within the method static than you can access it like this: foo::$variable; but that is mostly for outside use as its more strict with static values. Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918490 Share on other sites More sharing options...
mikesta707 Posted September 14, 2009 Share Posted September 14, 2009 it depends on your situation. if you want the constructor to assign variables that you define, IE class foo{ var $value; public function foo($value){ $this->value = $value; } } However, if whenever you instantiate the class, the variable will always have the same value, then assigning it in the declaration part and the constructor basically do the same thing Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918493 Share on other sites More sharing options...
RussellReal Posted September 14, 2009 Share Posted September 14, 2009 mike.. please re-read your posts before you submit.. or atleast preview them, look @ your code here lol and the last post you posted on.. the eval() demo.. there is a syntax error.. its not a race Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918495 Share on other sites More sharing options...
mikesta707 Posted September 14, 2009 Share Posted September 14, 2009 oh my bad, had to hurry to class so I was a bit rushed... but its just a missing semi colon and an extra $ symbol... its not that bad... everyone makes mistakes dude... Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918500 Share on other sites More sharing options...
KevinM1 Posted September 14, 2009 Share Posted September 14, 2009 To the OP, imagine you're creating your Person objects from data stored in the database... how likely is it that all of those people will be named Mr. Smith? Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918501 Share on other sites More sharing options...
Rommeo Posted September 14, 2009 Author Share Posted September 14, 2009 To the OP, imagine you're creating your Person objects from data stored in the database... how likely is it that all of those people will be named Mr. Smith? i just wanted to show the usage of both decleration. let me change the example ; <?php class brand { public $name; public $size; public function __construct() { $this->name = "not available"; $this->size = 5; } public function blabla etc.. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918513 Share on other sites More sharing options...
Rommeo Posted September 14, 2009 Author Share Posted September 14, 2009 Thank you for your explanations guys.. (: Now I get it Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918523 Share on other sites More sharing options...
RussellReal Posted September 15, 2009 Share Posted September 15, 2009 To the OP, imagine you're creating your Person objects from data stored in the database... how likely is it that all of those people will be named Mr. Smith? everybody with a black suit! if this was the matrix that is. Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918700 Share on other sites More sharing options...
KevinM1 Posted September 15, 2009 Share Posted September 15, 2009 To the OP, imagine you're creating your Person objects from data stored in the database... how likely is it that all of those people will be named Mr. Smith? everybody with a black suit! if this was the matrix that is. Hello, Mr. Anderson.... Quote Link to comment https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/#findComment-918833 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.