开发者

Call to undefined method when it's static and exists?

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 happe开发者_如何学JAVAn 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


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:

PHP considers each entry in the include path separately when looking for files to include. It will check the first path, and if it doesn't find it, check the next path, until it either locates the included file or returns with a warning or an error.

(Reposted my comment as an answer here, and expanded upon it, for future reference.)


You should use dirname(__FILE__) inside require_once(...). For example if you have a single folder with two files ActiveRecord.php and DB.php, and you want to reference DB from ActiveRecord, use the following format:

require_once(dirname(__FILE__).'/DB.php');


Of course. locate System.php

/opt/php-lib/System.php
/usr/share/php/System.php

I've changed the file name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜