I’m trying to do some PHP development but I can’t figure out how to reference my class in another PHP file. I am using CI as my MVC framework and under ‘Application’ I have created a folder called, ‘domain’ where I am placing my domain specific objects.
I have a class called ‘user’ defined in a file called ‘User.php’. So ‘domain/User.php’
I am trying to reference this class in another PHP file like this:
<?php
require('domain/User.php');
..
?>
This blows up with this error:
Failed opening required ‘domain/User.php’ (include_path=’.:/usr/lib/php’)
So, clearly, I don’t know how this is done in PHP. So can someone help me out?
Thanks!
You need to make sure you’re referencing the correct path. If you’re creating a new CI Controller (i.e. a controller that extends the core CI_Controller), you can easily solve this by using:
include (APPPATH . 'domain/User.php');Otherwise, you can find the absolute path of the current file by using:
Allowing you to reference whatever external file you want relative to the current file you’re using:
You can then reference all your external controllers and methods without any trouble. If you wanted to run function_a() in an external file, you’d need to use:
You could put these loads into a
__constructfunction to avoid repetition in your code.