php增加属性的方法:1、创建一个PHP示例文件;2、输入“error_reporting(-1);ini_set('display_errors','on');”;3、通过“$a->add();”给对象动态增加属性即可。

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑
php怎么增加属性?
php 给对象动态增加属性
示例代码
立即学习“PHP免费学习笔记(深入)”;
<?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);输出
world
object(A)#1 (3) {
["a"]=>
string(5) "hello"
["b"]=>
string(5) "world"
["c"]=>
string(4) "test"
}补充:对象可以调用对象所属类的静态方法,如
$a->p();
推荐学习:《PHP视频教程》
以上就是php怎么增加属性的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号