How to add attributes to php: 1. Create a PHP sample file; 2. Enter "error_reporting(-1);ini_set('display_errors','on');"; 3. Pass "$a- >add();" Just add attributes dynamically to the object.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to add attributes to php?
php Dynamically add attributes to the object
Sample code
<?php error_reporting(-1); ini_set('display_errors','on'); class A { public $a = 'hello'; public function add() { $this->b = 'world'; }- public static function p() { echo 'world',PHP_EOL; }- } $a = new A; $a->add(); $a->c = 'test'; $a->p(); var_dump($a);
Output
world object(A)#1 (3) { ["a"]=> string(5) "hello" ["b"]=> string(5) "world" ["c"]=> string(4) "test" }
Supplement: The object can call the static method of the class to which the object belongs. For example,
$a->p();
recommends learning: "PHP Video Tutorial"
The above is the detailed content of How to add attributes in php. For more information, please follow other related articles on the PHP Chinese website!