在类变量中存储类对象

时间:2012-01-28 22:33:49

标签: php oop class

我有两个类,一个抽象类和一个实际的功能类。我需要以某种方式将函数类存储在抽象类中,并使其可用于抽象类,但我遇到了困难。

下面是一些类似于我想要实现的代码

抽象类是与代码接口的抽象类,但是这可以加载几个实际处理联系权限服务器和管理权限逻辑的“插件”,但是我在加载和存储“插件”对象时遇到问题(如抽象类的第11行和第25行所示。

抽象类(auth.class.php):

<?php
    class AuthManager {
        private $userDatabase;

        public function __construct() {
            /**
             * AuthManager(0):      Constructor for the class. Will initiate an
             *                      auth session. Also autoconnects to the user
             *                      service. Defaulting to LDAP
             */
            $this ->userDatabase = new LDAPConnector();
        }
        public function login($user, $pass) {
            /**
             * login(2):            Abstraction function for the user database.
             *                      will check user and password against the 
             *                      user database and return the status
             * 
             * @param   string  $user:      Username for the user
             * @param   string  $pass:      Password for the user
             * @return  bool    $status:    1 on sucess, 0 on failure
             */
            if($user && $pass) {
                if($this->userDatabase->login($user, $pass)) {
                    return 1;
                }
            } else 
                return 0;
            }
        }
    }
?>

功能类(ldap.plug.php):

<?php
class LDAPConnector extends AuthManager {
    private $server = "192.168.0.3";
    private $connected = 0;
    private $connection = 0;
    private $domain = "morris";
    public function __construct($host = "192.168.0.3") {
        /**
         * LDAPConnector(1): Constructor for the class. Will connect to the server 
         * automatically unless told not to
         * @param string $host:     IPAddr/Hostname of AD server
         *                          Defaults to private var $server
         */
         if($host) {
             // Safety net to check we actually have a server to
             // connect to
             if(function_exists("ldap_connect")) {
                if($this->connection = ldap_connect($host)) {
                    $this->connected = 1;
                    return 1;
                }
            }
         } else {
             return 0;
         }
    }

    public function login($user,$pass, $domain = "") {
        /**
         * login(2):            Attempts to verify user/pass combo with
         *                      the LDAP server
         * 
         * @param string $user: Username to verify
         * @param string $pass: Password to verirf
         * @return bool       : 1 on sucess, 0 on failure
         */
        if($user && $pass) {

            // Verify we are actually connected to an LDAP server..
            if(!$this->connected) {
                // We cant possibly run queries on a server that doesnt
                // exist.. Exit with a failure.
                return 0;
            }

            if(!$domain) {
                // A domain needs to be specified for LDAP users..
                // If they havent given us append the default
                $user = $this->domain."\\".$user;
            } else {
                // Prepend the supplied domain..
                $user = $domain."\\".$user;
            }

            // Attempt to bind to the LDAP server. If returns true, the
            // uname/pwd combination was a good one.
            if(ldap_bind($this->connection, $user, $pass))
                return 1;
            else
                return 0;

        } else 
            return 0;
    }
}
?>

然后将这样调用:

<?php
$Auth = new AuthManager();

if($Auth->login("test", "123")) {
    echo 'Logged in';
}
?>

然而,当我尝试这个PHP吐出

Catchable fatal error: Object of class LDAPConnector could not be converted to string in /var/www/LDAP Experiment/libs/auth.class.php on line 26

有没有办法做我正在尝试的事情,还是我必须回到绘图板?

干杯

1 个答案:

答案 0 :(得分:1)

在auth.class.php中:

 public function login($user, $pass) {
            /**
             * login(2):            Abstraction function for the user database.
             *                      will check user and password against the 
             *                      user database and return the status
             * 
             * @param   string  $user:      Username for the user
             * @param   string  $pass:      Password for the user
             * @return  bool    $status:    1 on sucess, 0 on failure
             */
            if($user && $login) {
                echo $this->userDatabase;
                if($this->userDatabase->login($user, $pass)) {
                    return 1;
                }
            } else 
                return 0;
            }
        }

你有一个未定义的变量:在if语句中登录。这意味着什么?