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 :'(

Member Avatar for diafol

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.

@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 :)

Member Avatar for diafol

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.

many thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.