For example, I have a file error-status.php, which includes a function:
function validateHostName($hostName)
{
if ((strpbrk($hostName,'`~!@#$^&*()=+.[ ]{}\\|;:\'",<>/?')==FALSE) && !ctype_digit($hostName) && eregi("^([a-z0-9-]+)$",$hostName) && ereg("^[^-]",$hostName) && ereg("[^-]$",$hostName))
{
return true;
}
else
{
return false;
}
}
...
How do I call that function from a different PHP file after invoking require_once
?
require_once('error-status.php');
To use the functions written in one file inside another file include the import line, from filename import function_name . Note that although the file name must contain a . py extension, . py is not used as part of the filename during import.
To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.
In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems.
Include the file before you call the function.
include 'error-status.php';
validateHostName('myhostname');
I would simply extend the class or use the require /include
, then:
$var = new otherClass;
$getString = $var->getString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With