开发者

No echo output for PHP class_exists even though the class in on the page

I'm trying to get a message to display if a class is present, not sure if I've got the right idea with this or not but it doesn't appear to be showing anything even though the class is there on the page开发者_运维知识库.

<?php if (class_exists('quote-me')): ?>
<?php echo $this->__('View Quotation'); ?>
<?php endif; ?>

Can someone let me know if I'm on the right track.


class_exists() only checks if the class has been defined, not if you currently have an instance of that class. You could have the quote-me class defined and in scope, triggering the if block, but that by itself doesn't mean you have a valid instance of that class.

Assuming that $this->__('View Quotation'); works if you have an instance, then the fact that it's not working could be an indication that you actually don't have an instance of the quote-me class in scope.


from php.net :

This function checks whether or not the given class has been defined.

So, yes you are on the right track.

<?php if(class_exists('my_class_name')){
      // do something
}
?>


If this code isn't within the actual class then you should refer to the object by variable name which you assign to it at declaration/initialization, rather than $this. For example,

    $test = new quote-me
if (class_exists('quote-me')) 
{
     echo $test->__('View Quotation'); 
}


You should using class_exists(some_class, false) to prevent autoload...


Yes class_exists is the right function (if this is your question)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜