It's giving you errors because the constructor has required parameters. To make a parameter optional give it a default value like this function __construct($persons_name=null) { if ($persons_name) { $this->set_name($persons_name);//use the setter in the constructor. } } this will now work $stefan = new person(); $stefan ->set_name("Stefan Mischook"); $joel = new person("Joel Laviolette"); echo "The first Object name is: ".
$stefan->get_name(); echo "The second Object name is: ". $joel->get_name().
Cool. I understand this. Thanks!
– Joel May 15 '10 at 2:40.
A constructor is used to initialize an object. The expectation in object-oriented programming is that an object shouldn't exist unless it's in a valid state. For example, a Person without a first and last name might not be considered a valid entity, so when the object is first created it should be initialized with a first and last name in the constructor.
The reason you got an error is because the constructor has a required parameter, so you must pass an argument to it. P.S. I really hate explanations of object-oriented programming that try to use analogies like "Dog is-a Mammal". You should probably stay away from those examples.
They really give no helpful information in real world programming and sometimes even give students the illusion they understand how to use what they're being taught. If you're looking for a practical application of using a constructor to create an object in valid state, imagine a blog post that uses a database for persistence. For example, there would be no point writing a long post and then calling $BlogPost->save(); if the blog wasn't first initialized with a reference to the database.
The application of using a constructor in this case would perhaps be $BlogPost = new BlogPost($Database); It would make no sense to have to write: $BlogPost->setDatabase($Database); every time you wanted to do anything with it. Maybe you'd forget to write it once and you'd be wondering where the post you spent 30 minutes writing disappeared to. That's an example of an invalid state.
The idea is that you're including anything the class is dependent upon when it is first initialized, instead of risking the possibility of the object being in an invalid state. Edit: Corrected 'two parameters' to one.
IE, I could not do $BlogPost = new BlogPost(); – Joel May 15 '10 at 2:39 1 That's entirely up to how you define your constructor in the first place. Function __construct($persons_name) has one variable, $persons_name is the parameter.In my example, I just happened to do the same thing, although you could define it to have any number of arguments (or none). – Lotus Notes May 15 '10 at 2:41 +1 very nice and explanatory answer.
– Lars Andren May 15 '10 at 2:45 Thanks Byron. I accepted the other answer, because he more clearly answered my question of "do you always need to include an argument when creating an object? "-at least to my newbie brain-I learned a lot from you explanation as well!
– Joel May 15 '10 at 2:47 +1 nice explanation – RC. May 15 '10 at 2:50.
Php" , "templateDetails. Xml","images" and "css".
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.