Let’s say I’ve got two files class.php and page.php
class.php
<?php
class IUarts {
function __construct() {
$this->data = get_data('mydata');
}
}
?>
That’s a very rudamentary example, but let’s say I want to use:
$vars = new IUarts();
print($vars->data);
in my page.php file; how do I go about doing that? If I do include(LIB.'/class.php'); it yells at me and gives me Fatal error: Cannot redeclare class IUarts in /dir/class.php on line 4
You can use
include/include_onceorrequire/require_onceAlternatively, use autoloading
by adding to
page.phpIt also works adding that
__autoloadfunction in a lib that you include on every file likeutils.php.There is also this post that has a nice and different approach.
Efficient PHP auto-loading and naming strategies