Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php class not found when it is included

Tags:

php

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_URL{
...
       $class_name = "cPath_SEO_URL";
       return $class_name::href(); 
...
}

I am trying this in oscommerce.

Why is that?

like image 474
billaraw Avatar asked Feb 14 '11 14:02

billaraw


2 Answers

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.

like image 71
billaraw Avatar answered Oct 16 '22 22:10

billaraw


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(); 
like image 1
rik Avatar answered Oct 16 '22 21:10

rik