tags:

views:

49

answers:

1

This line of code:

echo "<strong> {$this->author->last} {$this->date->shortYear()}</strong> ...";

gives me this error:

Fatal error: Call to undefined method Date::shortYear() in /f5/debate/public/libs/Card.php  on line 22

Even though in Date.php (which is included in Card.php):

class Date {
    public $day;
    public $month;
    public $year;

    public function shortYear() {
        return substr($this->year, -2);
    }
}
A: 

there is a Date class in php. So my guess is you are instantiating the wrong Date class.

Change the name of the Date class in Card.php to say myDate;

Then try again $this->date = new myDate;

See how that works.

Create a separate file to test with to remove any other stuff that may be causing confusion

DC

Ignoring charles unnecessary downvote

Another way to test this is to go into Card.php and add or ammend the constructor for Date

function __construct() {
        echo "created date object\n";
}    

This will then print when you are creating the date object if it doesnt print then you know you are instantiating a different Date object.

Another method I often use is

$this->date = new Date;
var_dump($this->date);

If it shows a differnt class structure then you are expecting then again you have the wrong Date object

If not then look to see if $this->date is not redefined anywhere

e.g.

$this->date = new Date;

....

$this->date = new DateTime;

DC

DeveloperChris
The PHP date class is called `DateTime`, not Date.
Charles
Pear::Date actually http://pear.php.net/manual/en/package.datetime.date.examples.php Thanks for the unwarranted downvote, even if there wasnt a Date class in PHP its a valid method to test it
DeveloperChris
Charles' point is: "There is a Date class in PHP" is not the same as "There is a Date class in PEAR".
BoltClock
My Point is that there are classes called Date elsewhere. and it could have been included somewhere along the line. it doesn't matter whether its a php native class or a class from an extension or include. And it still doesn't warrant a downvote.
DeveloperChris