How to invoke a variable outside the class

Source: Internet
Author: User
How to call variables outside the class
This post was last edited by Hicoo on 2014-06-07 10:21:04

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

}
------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.
------Solution--------------------
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
Define all variables as constants in a.php
Define (' Db_host ', ' test ');
Direct use of db_host 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--------------------
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.
------Solution--------------------
Generally these configuration information is best to become constant
------Solution--------------------
Reference:
Thank you for the above two masters, and then I would like to put the code to get the variable in the constructor can be, because I will also use in other methods of some variables, it is not to be quoted 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

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);
}

}
?>


the second, with 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.