php class not found when it is included
I have a class in php called "SEO_URL". At a point in that class I have this
$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
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.
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_UR开发者_运维问答L{
...
$class_name = "cPath_SEO_URL";
return $class_name::href();
...
}
I am trying this in oscommerce.
Why is that?
Ok, you won't believe what was the problem.
I am used to open and close php file like this
<?
...
?>
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 <?php
not the <?
only.
It would load the class but it wouldn't interpret it as php.
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();
精彩评论