Understanding PHP Classes
I am trying to understand the built-in classes in PHP and how to use them. Also I am trying to use the correct language to describe these 'things'. Take for example the DateTime class.
So now I see a method on this class and it is denoted by DateTime::setDate . So am I correct in saying, "this is the DateTime class that has a method called setDate ? Also if you read the PHP manual on the DateTime class for setDate you find:
DateTime::setDate <-- does this mean I can use this as is in code? As in: DateTime::setDate(); ?
I do see how to create an object as in the below:
<?php $date = new DateTime(); $date->setDate(2001, 2, 3); echo $date->format('Y-m-d');// how would you know to do this? I thought $date->year; ?>
And this came from this: public DateTime DateTime::setDate ( int $year , int $month , int $day )
Also from the line directly above I should be able to figure out how to use it without seeing an example?
Any good input would be much appreciated.
I just hate the :: notation in reference, I always think I am dealing with a long static methods list.
Anyway it comes from C++ namespaces notation, so DateTime::diff is meant to be read "the function diff belongs to class DateTime", but it seems quite clear because I am on the DateTime reference page!
To keep things clearer, the "double colon" operator is called T_PAAMAYIM_NEKUDOTAYIM [hebrew for double colon actually].
Long story short, go with -> notation unless you read the static keyword in the method signature.
Unless a method is denoted as being static, you cannot invoke it in code with classname::methodname();. In code descriptions, the :: basically just says 'this method belongs to this class'. Yeah, it's kinda confusing.