Home  >  Article  >  Backend Development  >  How to modify public variables in PHP

How to modify public variables in PHP

PHPz
PHPzOriginal
2023-04-05 10:30:56785browse

PHP is a very popular programming language used for writing web applications. As an object-oriented programming language, PHP supports classes and objects and allows the definition of public, private and protected member variables. In PHP, public variables are member variables defined in a class and can be directly accessed and modified outside the class. In this article, we will discuss how to modify public variables in PHP.

First, let us take a look at how to define public variables in PHP. In the class, use the keyword "public" to define public variables, for example:

class MyClass {
  public $myPublicVar;
}

The above code defines a class named "MyClass", which has a public variable "myPublicVar".

Outside the class, you can access and modify public variables by creating objects and using the "->" operator, for example:

$obj = new MyClass;
$obj->myPublicVar = "Hello";
echo $obj->myPublicVar; // 输出 "Hello"

The above code creates an object named "$ obj" object and set the public variable "myPublicVar" to "Hello". Next, use the "echo" statement to output the value of "myPublicVar", which will output "Hello".

Now, let us discuss how to modify public variables in PHP. Generally speaking, you can modify an object simply by creating it and accessing the public variables. For example:

$obj = new MyClass;
$obj->myPublicVar = "World";
echo $obj->myPublicVar; // 输出 "World"

The above code sets the public variable "myPublicVar" to "World" and uses the "echo" statement to output its value, which will output "World". This is very similar to the previous example, the only difference is that the value of "myPublicVar" is set to "World" instead of "Hello".

Also, if you need to modify a public variable outside the class and cannot access it by creating an object, you can use the keyword "global" to access it. For example:

$myObj = new MyClass;
$myObj->myPublicVar = "Hello";
function myFunction() {
  global $myObj;
  $myObj->myPublicVar = "World";
}
myFunction();
echo $myObj->myPublicVar; // 输出 "World"

The above code creates an object named "$myObj" and sets the public variable "myPublicVar" to "Hello". Next, a function called "myFunction" is defined, which uses the "global" keyword to access the "$myObj" object and sets the value of "myPublicVar" to "World". Finally, use the "echo" statement to output the value of "myPublicVar", which will output "World".

In this article, we discussed how to use public variables in PHP. We saw how to define and access public variables, and how to modify them outside the class. Because public variables can be accessed and modified by other objects and functions, care needs to be taken when using them. When writing PHP code, be sure to take variable scope into account to avoid potential bugs and security issues.

The above is the detailed content of How to modify public variables in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn