In PHP, array is one of the very important data structures that can store multiple values. In PHP, arrays can be used as global variables, local variables or class attributes. Arrays are defined in a class, usually using public properties or private properties.
The following describes how to define arrays in PHP classes.
Public properties can be directly accessed inside and outside the class, so if you need to access the array outside the instantiated object of the class, you can use public properties. We can use the keyword public to define an array type property within a class.
For example:
class Test { public $array = array(); }
In the above example, we defined a public property named $array inside the class, whose type is an array. After instantiating an object of this class, the property can be accessed through the object:
$obj = new Test(); $obj->array[] = 'value1'; $obj->array[] = 'value2'; print_r($obj->array);
Output result:
Array ( [0] => value1 [1] => value2 )
Sometimes We don't want the instantiated object of the class to directly access the array properties, so we can use private properties to define the array. Private properties can only be accessed within the class.
For example:
class Test { private $array = array(); public function add($value) { $this->array[] = $value; } public function getArray() { return $this->array; } }
In the above example, we define a private property $array inside the class, whose type is an array. Two methods add and getArray are defined, where the add method is used to add elements to the array, and the getArray method is used to obtain the array. Since $array is a private property and cannot be accessed directly, elements must be added through the add method.
After instantiating an object of this class, you can access the add method and getArray method through the object:
$obj = new Test(); $obj->add('value1'); $obj->add('value2'); print_r($obj->getArray());
Output result:
Array ( [0] => value1 [1] => value2 )
A static property is a property that can be shared among all instances of a class. Using static properties to define an array allows multiple objects to share an array.
For example:
class Test { private static $array = array(); public static function add($value) { array_push(self::$array, $value); } public static function getArray() { return self::$array; } }
In the above example, we define a private static property $arra inside the class, whose type is an array. Two static methods add and getArray are defined, where the add method is used to add elements to the array, and the getArray method is used to obtain the array. Since $array is a static property, it can be accessed through the self:: keyword.
After instantiating an object of this class, you can directly access the add method and getArray method through the object:
Test::add('value1'); Test::add('value2'); print_r(Test::getArray());
Output result:
Array ( [0] => value1 [1] => value2 )
Summary
You can use public properties, private properties or static properties to define arrays in PHP classes. The specific method to use needs to be decided based on the characteristics of the class and the business scenario. In addition to the three methods introduced above, you can also use class constants or constructors. In actual development, choosing the appropriate way to define an array not only allows you to use arrays conveniently and quickly, but also makes the code clearer and easier to read.
The above is the detailed content of How to write array in php class. For more information, please follow other related articles on the PHP Chinese website!