1. 程式人生 > >php 給對象動態增加屬性

php 給對象動態增加屬性

() isp start ati 增加 word obj sta art

    1. <?php
    2. error_reporting(-1);
    3. ini_set(‘display_errors‘,‘on‘);
    4. class A {
    5. public $a = ‘hello‘;
    6. public function add() {
    7. $this->b = ‘world‘;
    8. }-
    9. public static function p() {
    10. echo ‘world‘,PHP_EOL;
    11. }-
    12. }
    13. $a = new A;
    14. $a->add();
    15. $a->c = ‘test‘;
    16. $a->p();
    17. var_dump($a);
      1. world
      2. object(A)#1 (3) {
      3. ["a"]=>
      4. string(5) "hello"
      5. ["b"]=>
      6. string(5) "world"
      7. ["c"]=>
      8. string(4) "test"
      9. }

php 給對象動態增加屬性