开发者

How do I access these PHP class variables?

I have two php files, one manages databa开发者_JAVA百科se connection and the other retrieves data from the database. I am writing this from scratch as a learning experience, and granted it is 5am but for some reason I cannot access the variables I need to.

My database connection file is as follows:

<?

class mysqlManager {
    var $dbhost = 'xxx.xxx.xxx.xxx';
    var $dbuser = 'xxx';
    var $dbpass = 'xxx';
    var $dbname = 'xxx';

    var $connection;
    var $errorCode;
    var $errorMsg;

    public function __construct($host='',$user='',$pass='',$name='') {
        if(!$host=='') $this->dbhost = $host;
        if(!$user=='') $this->dbuser = $user;
        if(!$pass=='') $this->dbpass = $pass;
        if(!$name=='') $this->dbname = $name;
    }

    function openConnection($host,$user,$pass) {
        if(!$this->connection = @mysql_connect($host,$user,$pass,true)) {
            $this->errorCode = mysql_errno();
            $this->errorMsg = mysql_error();
            return false;
        }
        return true;
    }

    function closeConnection() {
        if($this->connection){
            @mysql_close($this->connection);
        }
    }

    function selectDB($name) {
        if(!$this->openConnection($this->dbhost,$this->dbuser,$this->dbpass)){
            return false;
        }else{
            return @mysql_select_db($name);
        }
    }
}

?>

The next file for getting data is as follows:

<?

class ccp {
    var $mgr;

    public function __construct() {
        $this->mgr = new mysqlManager();
    }

    public function test() {
        print_r($this->mgr);
    }

    function getCCP() {
        if($mgr->openConnection($mgr->dbhost,$mgr->dbuser,$mgr->dbpass)) {
            if(!$mgr->selectDB($mgr->dbname)) {
                $mgr->closeConnection();
                return 'An error has occured while processing your request.';
            }
            $q = 'SELECT * FROM ccp WHERE cat="ccp" ORDER BY date DESC';
            $r = @mysql_query($q);
            $ret='';
            while($row = @mysql_fetch_array($r)){
                $ret  = '<div class="post">';
                $ret .= '   <h2 class="title">'.$row["title"].'</h2>';
                $ret .= '   <p class="date">'.$row["date"].'</p>';
                $ret .= '   <div class="entry">'.$row["body"].'</div>';
                $ret .= '</div>';
            }
            $mgr->closeConnection();
            return $ret;
        }
    }
}

?>

When I run the test function, I get this:

mysqlManager Object ( [dbhost] => xxx.xxx.xxx.xxx [dbuser] => xxx [dbpass] => xxx [dbname] => xxx [connection] => [errorCode] => [errorMsg] => )

How do I access the variables in the mysqlManager Object?

Thanks!


To access the members of an object, use ->, with nested objects, multiple times. So: In test():

echo $this->mgr->dbhost;  // echoes xxx.xx.xxx.xxx
echo $this->mgr->dbpass;  // echoes xxx

You can do this because the variables were declared using var, making them implicitly public. If you declare them with private or protected like so:

class mysqlManager
{

 private $dbhost = 'xxx.xxx.xxx.xxx';
 protected $dbuser = 'xxx';
 ...

you will not be able to access the variables from another object.

Does that answer your question?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜