开发者

"Call to undefined method" - but I know it exists

This line of code:

echo "<strong&开发者_运维知识库gt; {$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);
    }
}


You're instantiating the wrong Date class. You can use PHP's get_class_methods() function to confirm which methods are available.


If you work in an setup where you automatically upload your changes from your IDE to the web server your checking your pages on. It might accidentally be the case that it didn't upload the file.


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


I know this is an old question, but I just battled this a bit.

I'm using Netbeans to code on a remote server, it turns out that for one reason or another, it wasn't syncing a file to the remote host.

This was particularly annoying because I could ctrl-click into the class and see the method, and stepping into a different method in the same class would open up the seemingly correct file.

After I discovered the issue, I simply right clicked the file and clicked 'upload.'


You must also check the namespace used. You can have two classes with the same name, but from different namespaces. If two classes were created in the same namespace with the same name. Then what class got constructed or accessed would be dependant on the order in which they were interpreted.

Also in regard to those Date problems. If you want to access the core namespace you would reference

new \DateTime();

instead of new DateTime();


For putting in my two cents:

It has happened to me by having two plugins in wordpress with the same namespace/class name but different methods in each:

/Utils/Tools

Deactivating one of the plugins fixed it.

The solution is to create a root namespace for each plugin:

/Plugin1/Utils/Tools
/Plugin2/Utils/Tools
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜