Q:

Include a Class from another file in PHP

belongs to collection: PHP Miscellaneous

0

Include a Class from another file in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

To include a class in PHP, we can use any of the include/include_once or require/require_once methods. In this example, we will create the function in function.php file and then import it in index.php file.

Content of function.php:

<?php
class myNewClass {
	<!-- Function Goes Here -- >
}
?>

Content of Index.php:

<?php
require('function.php');
$vars = new myNewClass();
?>

Here, when a user visits index.php, upon initialization, function.php is called (due to require) and then treated as a part of index.php. Now, index.php can call functions from function.php as well.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Miscellaneous

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Include php files when they\'re in different ... >>
<< print (get) file extension in PHP...