jim dif
jim dif

Reputation: 641

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.

Thanks, Jim

Upvotes: 0

Views: 230

Answers (4)

Vahid
Vahid

Reputation: 584

From PHP help you can find this expression: "Sometimes it is useful to refer to functions and variables in base classes or to refer to functions in classes that have not yet any instances. The :: operator is being used for this."
The :: operator used to call the function of base class and ignores classes that made from base class that overrides the method.

<?php
class A {
    function example() {
        echo "I am the original function A::example().<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am the redefined function B::example().<br />\n";
        A::example();
    }
}

// there is no object of class A.
// this will print
//   I am the original function A::example().<br />
A::example();

// create an object of class B.
$b = new B;

// this will print 
//   I am the redefined function B::example().<br />
//   I am the original function A::example().<br />
$b->example();
?> 

Upvotes: 0

moonwave99
moonwave99

Reputation: 22817

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.

Upvotes: 1

Major Productions
Major Productions

Reputation: 6042

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.

Upvotes: 1

Tchoupi
Tchoupi

Reputation: 14681

Actually I never understood why they use the :: operator in the documentation and I find it confusing.

But when the documentation says DateTime::setDate it means that this is the method definition. You must first instantiate the class with:

$instance = new DateTime();

Then call the method on the instance:

$instance->setDate($dateString);

This is how methods work on class objects, and it is written as Classe::method.

Upvotes: 0

Related Questions