<?php header("Content-type: text/html; charset=utf-8"); class person{ //成员属性 private $name; private $age = 20; //成员方法 function username($name){ return $this->name=$name; } //__set()方法用来设置私有属性 function __set($property_name, $value) { echo "在直接设置私有属性值的时候,自动调用了这个 __set() 方法为私有属性赋值<br />"; $this->$property_name = $value; } //__get()方法用来获取私有属性 function __get($property_name) { echo "在直接获取私有属性值的时候,自动调用了这个 __get() 方法<br />"; } } $obj= new person(); echo $obj->username("张三"); echo "<hr/>"; $obj->name = "李四";//在直接设置私有属性值的时候,自动调用了这个 __set() 方法为私有属性赋值 echo "<hr/>"; echo $obj->age;//在直接获取私有属性值的时候,自动调用了这个 __get() 方法,不设置get特殊方法就会报下面的错 //echo $obj->age;//当成员属性或者方法前有private修饰,不能在类外访问。否则报错如下 /* Fatal error: Cannot access private property person::$age in D:\wamp\www\ajax\index.php on line 23 */ ?>
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks agoByDDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.