Howto:Start using vectors and hashes in Nasal: Difference between revisions

Jump to navigation Jump to search
No edit summary
Line 543: Line 543:


Now, let's imagine you want to add another member function to print a certain field's value, such as "number".  
Now, let's imagine you want to add another member function to print a certain field's value, such as "number".  
Given that objects created from the same class template, there needs to be a way for a function to refer to itself, i.e. to refer to the object itself, otherwise the function wouldn't know what object is meant.
Given that objects created from the same class template, there needs to be a way for a method (class function) to refer to itself (its own object), i.e. to refer to the object itself, otherwise the function wouldn't know what object is meant, and what object it is referring to.
 
This can be accomplished using the "me" keyword which ensures that the member function is always referring to the currently active object:


This can be accomplished using the "me" keyword which ensures that the member function is always referring to the currently active object (itself):


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Line 565: Line 564:
};
};
</syntaxhighlight>
</syntaxhighlight>
Note how we are really just prepending the "me" keyword to the object's field, making it clear that "me" is the requested namespace/context of the field to be retrieved.


On the other hand, maybe you'd like to add some information (such as the number) during construction time to the object. So this would require changing the constructor function to accept a parameter, too:
On the other hand, maybe you'd like to add some information (such as the number) during construction time to the object. So this would require changing the constructor function to accept a parameter, too:

Navigation menu