php > require_once('System.php');
php > System::users();
PHP Fatal error: Call to undefined method System::users() in php shell code on line 1
This seems to happen both from interactive command-line and from the a simple script:
<?php
require_once('System.php');
var_dump(System::users());
?>
System.php is in the PHP include path – and includes properly:
php > echo (class_exists('System')) ? 'THERE!' : 'WHERE?';
WHERE?
php > require_once('System.php');
php > echo (class_exists('System')) ? 'THERE!' : 'WHERE?';
THERE!
php > System::users();
PHP Fatal error: Call to undefined method System::users() in php shell code on line 1
I'm having a hard time grasping why it says there is a call to an undefined method when in fact it's static, it's there, and should be accessible.
<?php
class System
{
public static function users()
{
$users = array();
if( !$data_array = file('/etc/passwd') )
{
return false;
}
foreach( $data_array as $line )
{
$data = explode(":", $line);
$user = array_shift($data);
list(, $uid, $gid, $info, $path, $terminal) = $data;
$tmp = array();
$tmp['uid'] = $uid;
$tmp['gid'] = $gid;
$tmp['name'] = array_shift(explode(',', $info));
$tmp['path'] = $path;
$tmp['terminal'] = $terminal;
$users[$user] = $tmp;
unset($tmp);
}
return $users;
}
public static function user( $user )
{
$users = self::users();
if( array_key_exists($user, $users) )
{
return $users[$user];
}
else
{
return false;
}
}
}
?>
PHP 5.3.2-1ubuntu4.5 with Suhosin-Patch (cli) (built: Sep 17 2010 13:49:46)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
Best Answer
Make sure you're including the right file - perhaps an echo somewhere in the class file and see if it echoes when included. If it wasn't echoed, you're getting the wrong
System.php
file.If there is a file that matches the name that you specify in more than one of the include paths, PHP will just pick it up from whatever include path came first. That may or may not be the include path that you expect your files to be read from, which is why I don't ever rely on that setting and just specify all my includes absolutely. From the manual:
(Reposted my comment as an answer here, and expanded upon it, for future reference.)