add();" Just add attributes dynamically to the object."/> add();" Just add attributes dynamically to the object.">

Home  >  Article  >  Backend Development  >  How to add attributes in php

How to add attributes in php

藏色散人
藏色散人Original
2022-01-11 10:18:103659browse

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.

How to add attributes in php

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(&#39;display_errors&#39;,&#39;on&#39;);
 
class A { 
    public $a = &#39;hello&#39;;
 
    public function add() {
        $this->b = &#39;world&#39;;
    }-  
    public static function p() {
        echo &#39;world&#39;,PHP_EOL;
    }-  
}
 
$a = new A;
$a->add();
$a->c = &#39;test&#39;;
$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!

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