php global within a class but outside a function?
I can access $conn from within my fun开发者_开发问答ction like so:
function xyz($a){
global $conn;
....
}
I am wondering if there is a way to do this within a class ex.
class abc
{
global $conn;
public function xyz($a)
{
$conn->execute...
}
public function xya($a)
{
$conn->execute...
}
}
The above method gives me an error. I know I can access it like so:
class abc
{
public function xyz($a)
{
global $conn;
$conn->execute...
}
public function xya($a)
{
global $conn;
$conn->execute...
}
}
I hope you can understand what I mean. Thanks in advance for the help.
Your second example is possible, your first isn't. There is no such thing as having a member variable of a class that is a global variable, or of using scope like you are in your first example.
Generally, however, I would recommend to avoid using the global keyword completely. I do a lot of PHP programming and have never used it in anything serious (indeed, anything at all in the last 10 or so years).
If you need to be able to access a variable throughout your class, I'd pass the variable in to your class constructor, like this:
class abc
{
private $conn;
function __construct($conn)
{
$this->conn = $conn;
}
public function xyz($a)
{
$this->conn->execute();
}
public function xya($a)
{
$this->conn->execute();
}
}
Then you'd have
$myabc = new abc($conn);
That way, you're being explicit about what variable is available to what functions. It also makes it easier/possible in future to give different values of $conn to different classes or even rename what $conn is, and yet you won't have to go through all your classes and change each reference to that variable.
With global variables, it is a lot harder to track which variable is available to which functions, or do it in a structured way. Also, there's more temptation to keep piling on more dependencies on global variables until you lose the benefit of having classes.
Assign it to a class variable.
$conn = 'something';
class abc
{
private $conn;
public function __construct($conn) {
$this->conn = $conn;
}
public function xyz($a)
{
$this->conn->execute...
}
public function xya($a)
{
$this->conn->execute...
}
}
Instead of assigning it in the constructor, you can also use a setter method such as:
public function setConn($conn) {
$this->conn = $conn;
}
or (though I do not recommend this) set the variable to public scope within the class which lets you change it at will:
class abc{
public $conn;
...
}
$abc = new abc;
$abc->conn = $conn;
You cannot have global as a modifier .
You can do what you want in one the following was :
$con;
class abc
{
private $conn;
public function __construct($conn) {
$this->conn = $con;
} .....
}
OR
class abc
{
static public $conn; ....
}
And for one of the object you instantiate it instead on instantiating for the global .
It's dirty (I do not like "global" at all) but it should work:
// ...
private $var;
public function foo() {
global $var;
$this->var = $var;
}
// ...
Yes, just change global
to var
or public
,private
,protected
.
Example:
class myClass {
public $myClassVar = "Apples";
public __construct(){
echo( $this->myClassVar );
}
}
For more on visibility of these variables (public,private,protected), see this page on PHP.net.
精彩评论