I am working through this tutorial:
Firstly, you have a setter and gator The method has been created in the class:
And then you create an object and the results resonate:
Works as expected, and I understand.
Then he introduces construct:
class person {var $ name; Function __ composition ($ persons_name) {$ this- & gt; Name = $ persons_name; } Function set_name ($ new_name) {$ this- & gt; Name = $ new_name; } Get_name () function {return $ this- & gt; Name; }}
and so on:
It's all right and it makes sense.
Then I tried to combine both and I got an error, so I'm curious - always a consortor is the place of a "mill" ceremony? If you have a constructor, do you always need to include an argument in creating an object?
Errors:
& lt ;? Php $ stefan = new person (); $ Jimi = new person (); $ Jol = new person ("Joel Liviellet"); $ Stephen - & gt; Set_name ("stephen mirchuk"); $ Jimi - & gt; Set_name ("Nick Waddles"); Echo "is the first object name:". $ Stefan- & gt; Get_name (); The echo is "the second object name:". $ Jimmy- & gt; Get_name (); The echo is "third object name:". $ Joel- & gt; Get_name (); ? & Gt;
This is giving you errors because the constructor requires parameters to make a parameter optional Give it a default value like
function __ composition ($ person_name = null) {if ($ persons_name) {$ this- & gt; Set_name ($ person_name); // Use Setter in the Creator}}
This will work now
$ stefan = new person (); $ Stephen - & gt; Set_name ("stephen mirchuk"); $ Jol = new person ("Joel Liviellet"); Echo "is the first object name:". $ Stefan- & gt; Get_name (); Echo "is the second object name: $. Joel-> get_name ();
Comments
Post a Comment