php 包含时找不到php类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4993385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:29:11  来源:igfitidea点击:

php class not found when it is included

php

提问by billaraw

I have a class in php called "SEO_URL". At a point in that class I have this

我在 php 中有一个名为“SEO_URL”的类。在那堂课的某个时刻,我有这个

$class_name = "cPath_SEO_URL";
return $class_name::href(); 

and I get

我得到

Fatal error: Class 'cPath_SEO_URL' not found in
...\includes\classes\seo.class.php on line 52

The thing is I have included the class on top of SEO_URL

问题是我在 SEO_URL 之上包含了这个类

include_once(/path/to/my/class);
class SEO_URL{

}

and I get that error.

我得到了那个错误。

However, when I hard-code the class on top of the class SEO_URL it works. So this works.

但是,当我在类 SEO_URL 之上对类进行硬编码时,它会起作用。所以这是有效的。

class cPath_SEO_URL{
    function cPath_SEO_URL(){}
    function href() { return "CPathHref"; }
}
class SEO_URL{
...
       $class_name = "cPath_SEO_URL";
       return $class_name::href(); 
...
}

and this doesn't

而这并没有

include_once(/path/to/my/class);
class SEO_URL{
...
       $class_name = "cPath_SEO_URL";
       return $class_name::href(); 
...
}

I am trying this in oscommerce.

我正在 oscommerce 中尝试这个。

Why is that?

这是为什么?

回答by billaraw

Ok, you won't believe what was the problem.

好吧,你不会相信这是什么问题。

I am used to open and close php file like this

我习惯像这样打开和关闭php文件

<?
   ...
?>

not

不是

<?php

?>

and the class file was without the <?php .. ?>tag but the <? ... ?>tag. I guess the environment I am working in now wanted the <?phpnot the <?only.

并且类文件没有<?php .. ?>标签而是<? ... ?>标签。我想我现在工作的环境想要的<?php不是<?唯一的。

It would load the class but it wouldn't interpret it as php.

它会加载该类,但不会将其解释为 php。

回答by rik

With

$class_name = "cPath_SEO_URL";
$test = new $class_name();
return $test::href();

you're making a static call on an instance. That doesn't make sense.
Instead you'll want to do

您正在对实例进行静态调用。那没有意义。
相反,你会想要做

$class_name = "cPath_SEO_URL";
return $class_name::href();