0

I have a very simple class for date display:

class timing {

    public $today;
    public $year;

    function __construct() {
        date_default_timezone_set('GMT');
    }

    function now() {
        return $this->today = date("D j M Y");
    }

    function year() {
        return $this->year = date("Y");
    }

}

I am pulling in using:

$x = new timing();
echo $x->today();

But i get nothing back. What am i missing :'(

2
Contributors
4
Replies
18
Views
4 Years
Discussion Span
Last Post by Squidge
2

You're calling a method called today() instead of calling the property today. Try:

echo $x->today;

However, it doesn't seem that you've set this:

function now() {
    return $this->today = date("D j M Y");
}

In order to get today, you ned this method to run. You don't run it from the constructor and your client code doesn't run it, so $today will be empty.

Edited by diafol

0

@diafol,

Many thanks for getting back.

D'oH is all i have to say, was staring at it and couldnt see he wood for the trees :)

$x = new timing();
echo $x->now();

Now working thanks :)

0

Ok, mark as solved if so. BTW - all your props and methods are public. If you don't need them to be publically accessible from the client code, consider them as private, or if they are going to be used by extended classes, protected.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.