{$this->author->last} {$this->date->shortYear()}

"Name the method undefined", but I know that it exists

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 if 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);
    }
}
+3
source share
5 answers

You are creating the wrong class Date. You can use the PHP function get_class_methods()to confirm which methods are available.

+3
source

there is a Date class in php. Therefore, I assume that you are creating an invalid Date class.

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

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

See how it works.

Create a separate verification file to remove any other things that might cause confusion.

DC

Ignoring charles of unnecessary downvote

- Card.php Date

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

, , , Date.

, ,

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

differnt, , Date

, , $this- > date -

.

$this->date = new Date;

....

$this->date = new DateTime;

DC

0

, , .

Netbeans , , .

, Ctrl , , , .

, , "".

0

, IDE -, . , , .

0

. , . . , , .

. ,

new \ DateTime ();

instead of the new DateTime ();

0
source

Source: https://habr.com/ru/post/1754169/


All Articles