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.
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:
Content of Index.php:
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