stormchaser
July 23rd, 2010, 12:34 AM
My goal is to have functions defined within include statements so that I can access my functions from anywhere on my site. If I do this:
function.php
<?php ;
function helloworld() {
echo "Hello World\n";
}
code.php
<?php
require_once "function.php";
helloworld();
?>
I get a wonderful "Call to undefined function error".
If I do this instead:
function.php
<?php ;
class class1 {
public static function helloworld() {
echo "Hello World\n";
}
}
code.php
<?php
require_once "function.php";
class1::helloworld();
?>
I get the output as desired. Without the semicolon right after the <?php tag in either example, I get the "Call to Undefined Function" error. I have no idea why the example using the class and the semicolon in the first line works when just defining the function in the first example doesn't (even with the semicolon). Maybe I'm just missing something obvious here, but I'd really like to know why this behaves as it does?
function.php
<?php ;
function helloworld() {
echo "Hello World\n";
}
code.php
<?php
require_once "function.php";
helloworld();
?>
I get a wonderful "Call to undefined function error".
If I do this instead:
function.php
<?php ;
class class1 {
public static function helloworld() {
echo "Hello World\n";
}
}
code.php
<?php
require_once "function.php";
class1::helloworld();
?>
I get the output as desired. Without the semicolon right after the <?php tag in either example, I get the "Call to Undefined Function" error. I have no idea why the example using the class and the semicolon in the first line works when just defining the function in the first example doesn't (even with the semicolon). Maybe I'm just missing something obvious here, but I'd really like to know why this behaves as it does?