How to call variables outside the class

Source: Internet
Author: User
a.php page is a data link variable
$dbhost = "localhost";
$dbname = "FFFF";
$dbuser = "CCCC";
$dbpwd = "123456";
$dbprefix = "User";
$db _language = "GBK";

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

Link Database
function OpenData ($database) {
In this method, how to get the variables outside the class
}


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"];
}

}


Reply to discussion (solution)

Link Database
function OpenData ($database) {
Include (".. /data/a.php ");
}

or defined as constants

You are outside the class definition require_once (".. /data/a.php ")
Causes database-related parameters to become global variables and may overwrite variables with the same name
It can also be easily observed by $GLOBALS arrays.

You need to see the basics.

Require_once (".. /data/a.php "); class MyDatabase {    //Link database    function opendata ($database) {        //method One        global $dbhost, $ dbname, $dbuser, $dbpwd, $dbprefix, $db _language;        Method two        require_once (".. /data/a.php ");        Method three        //in a.php the variables are all defined as constant        define (' db_host ', ' test ');        When calling, use Db_host        echo db_host directly;    }    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"];}    }  

Thank you for the above two ace, and then I want to put the code that gets the variable in the constructor, because I also use some variables in other methods, that is not to quote again, but how many variables of the constructor return (return)?
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;
Can you tell me how many variables are returned, and if there is no return, the other method cannot get
Echo $dbhost;
}

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

}

It is recommended that the variables of the a.php be written in the same array, or changed into function and return
Naming pollution is sometimes disgusting.

Generally these configuration information is best to become constant

Thank you for the above two ace, and then I want to put the code that gets the variable in the constructor, because I also use some variables in other methods, that is not to quote again, but how many variables of the constructor return (return)?
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;
Can you tell me how many variables are returned, and if there is no return, the other method cannot get
Echo $dbhost;
}

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

}


The first method, with constants, can be used everywhere.
.. /data/a.php
 
  

 
  Myhost = Myhost;        $this->myuser = myuser;        $this->mypwd = MyPwd;    }    function OpenData ($database) {        $linkid = mysql_connect ($this->myhost, $this->myuser, $this->mypwd);}    }? >


the second, with the config array
.. /data/a.php
 
  ' localhost ',    ' myuser ' = ' cccc ',    ' mypwd ' and ' 123456 ');

 
  Dbconfig = require_once (".. /data/a.php ");    }    function OpenData ($database) {        $this->__construct ();        $linkid = mysql_connect ($this->dbconfig[' myhost '), $this->dbconfig[' MyUser '], $this->dbconfig[' mypwd ']);}    }? >

To Fdipzone:
I press your second method with an array, and then a bit of a problem, when debugging at the end of the constructor output array print_r ($this->dbconfig);
The output is: Array ([myhost] = localhost [myuser] = CCCC [MyPwd] = 123456) array ([myhost] = [MyUser] = [m YPWD] =)
The output is two arrays, the first is normal, the second value is empty,
Then I output the array in method OpenData: $this->__construct (); Print_r ($this->dbconfig); The output in this method is displayed only: Array

Solving?

To Fdipzone:
After testing again, your second method uses an array to change the require_once to include, then everything is OK and the problem is solved.

#6 the code for the second scenario is wrong!
Although it is a sample code, but since it is taught how to do, how to say it has to be tested it?

1. The load parameter uses require_once, which means that the loaded data will only be loaded once
Then, when you need to instantiate two MyDatabase, only the first one can get the parameters
2, in OpenData executed the $this->__construct () based on Error 1, when the execution MyDatabase method is, due to failure to load the parameter file, will cause the database connection does not

The correct wording is

Class mydatabase{  private $linkid;  Public Function __construct () {    $dbConfig = include (".. /data/a.php ");    $linkid = mysql_connect ($dbConfig [' Myhost '], $dbConfig [' MyUser '], $dbConfig [' mypwd ']);  }  function OpenData ($database, $charset = ' utf8 ') {    mysql_select_db ($database, $this->linkid);    mysql_query ("Set names $charset", $this->linkid);}  }

1. The parameter file file is sensitive and should not be saved in the object. Load when needed, throw it out
2. A database class corresponds to only one database connection, so the database connection should be done in the constructor
3, the OpenData method is only responsible for selecting the database and character set settings, the task is single

Thank you for the version and master of the enthusiastic help, very very much thank you!!

  • Related Article

    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.