Home > Backend Development > PHP Problem > How to add attributes in php

How to add attributes in php

藏色散人
Release: 2023-03-14 19:20:02
Original
3678 people have browsed it

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);
Copy after login

Output

world
object(A)#1 (3) {
  ["a"]=>
  string(5) "hello"
  ["b"]=>
  string(5) "world"
  ["c"]=>
  string(4) "test"
}
Copy after login

Supplement: The object can call the static method of the class to which the object belongs. For example,

$a->p();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template