How to call variables outside the class

Source: Internet
Author: User
How to call variables outside the class this post is finally edited by hicoo from 2014-06-0710: 21: 04. the php page is the data link variable $ dbhost & nbsp; localhost; $ dbname & nbsp; ffff; $ dbuser & nbsp; cccc; $ dbpwd & how to call variables outside the class

This post was last edited by hicoo at 10:21:04 on

A. The php page is a data link variable.
$ Dbhost = "localhost ";
$ Dbname = "ffff ";
$ Dbuser = "cccc ";
$ Dbpwd = "123456 ";
$ Dbprefix = "user ";
$ Db_language = "gbk ";

B. The php page class contains the operation functions of various databases.
Require_once ("../data/a. php ");
Class mydatabase {

// Link to the database
Function opendata ($ database ){
How can I obtain all the variables outside the class in this method?
}


// Query a record
Function ReadOne ($ database ){
Self: opendata ($ database );
$ Rs = mysql_query ("SELECT * from fcc where id = 1 ");
$ Row = mysql_fetch_array ($ rs );
Self: closedata ();
Return $ row ["date"];
}

}
------ Solution --------------------
// Link to the database
Function opendata ($ database ){
Include ("../data/a. php ");
}

Or define it as a constant.

Require_once ("../data/a. php ")
Changes database-related parameters to global variables, and the variables with the same name may be overwritten.
You can also use the $ GLOBALS array to easily observe
------ Solution --------------------
You need to look at the basics.

Require_once ("../data/a. php ");

Class mydatabase {

// Link to the database
Function opendata ($ database ){
// Method 1
Global $ dbhost, $ dbname, $ dbuser, $ dbpwd, $ dbprefix, $ db_language;
// Method 2
Require_once ("../data/a. php ");
// Method 3
// Define all variables as constants in a. php.
Define ('Db _ host', 'test ');
// Use DB_HOST directly when calling
Echo DB_HOST;
 
}


// Query a record
Function ReadOne ($ database ){
Self: opendata ($ database );
$ Rs = mysql_query ("SELECT * from fcc where id = 1 ");
$ Row = mysql_fetch_array ($ rs );
Self: closedata ();
Return $ row ["date"];
 
}

}

------ Solution --------------------
We recommend that the variables in a. php be written into the same array, or you can change them to function and return the array.
Naming pollution is sometimes disgusting.
------ Solution --------------------
Generally, it is best to change the configuration information to a constant.
------ Solution --------------------
Reference:
I am very grateful to the above two experts. can I put the code for getting the variables in the constructor? because some variables will also be used in other methods, so it is not necessary to reference them again, but how does one return multiple variables of the constructor?
Class mydatabase {
Private $ myhost;
Private $ myuser;
Private $ mypwd;

Public function _ construct (){
Require_once ("../data/a. php ");
$ This-> myhost = $ dbhost;
$ This-> myuser = $ dbuser;
$ This-> mypwd = $ dbpwd;
Return $ this-> myhost;
How can I return multiple variables? if there is no return in the test, other methods cannot be obtained.
// Echo $ dbhost;
}

Function opendata ($ database ){
$ This->__ construct ();
$ Linkid = mysql_connect ($ this-> myhost, $ this-> myuser, $ this-> mypwd );
}

}

The first method is to use constants so that they can be used anywhere.
../Data/a. php

define('MYHOST', 'localhost');
define('MYUSER', 'cccc');
define('MYPWD', '123456');
?>


require_once("../data/a.php");

class mydatabase{

private $myhost;
private $myuser;
private $mypwd;

public function __construct(){
$this->myhost = MYHOST;
$this->myuser = MYUSER;
$this->mypwd = MYPWD;
}

function opendata($database){
$linkid = mysql_connect($this->myhost, $this->myuser, $this->mypwd);
}

}
?>


Second, use the config array
../Data/a. php

return array(
'myhost' => 'localhost',
'myuser' => 'cccc',
'mypwd' => '123456'
);


class mydatabase{

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.