PHP object directly accesses private properties_PHP tutorial
PHP object directly accesses private properties
<?php header("content-type:text/html;charset=UTF-8"); class Person{ //私有的成员属性,对直接访问象 private $name; private $age; private $sex; //魔术方法 __construct(), __set(), __unset(), __isset(), __unset()..... function __construct($name="name1",$age =20,$sex="女"){ $this->name=$name; $this->age=$age; $this->sex=$sex; } /* 输出 Cannot access private property Person::$name 对象不能直接访问和设置私有属性的值,但是通过魔术方法__get($proName), __set($proName,$proValue)可以做到. 步骤: 1.重写魔术方法__get($property) , __set($proName,$proValue) 2.用对象直接访问或设置私有属性 $p1->name="对象直接访问私有属性"; echo $p1->name; 3.对象直接访问或设置私有属性时,会自动调用魔法方法__get($proName), __set($proName,$proValue) */ function __get($proName){ return $this->$proName; } function __set($proName,$proValue){ $this->$proName=$proValue; } function say(){ echo "$this->name:我的年龄$this->age,性别:$this->sex<br>"; } function run(){ $this->left(); $this->right(); } private function left(){ echo "left"; } private function right(){ echo "right"; } //析构方法,对象销毁前自动调用 function __destruct(){ echo "$this->name:我走了<br>"; } } $p1 = new Person("name1",15,"女"); $p2 = new Person("name2",20,"男"); $p3 = new Person("name3",30,"女"); /* 对象直接访问或设置私有属性 */ $p1->name="对象直接访问私有属性"; echo $p1->name."<br>"; /* 输出,注意__destruct()的输出顺序 name1:我的年龄15,性别:女 name2:我的年龄20,性别:男 name3:我的年龄30,性别:女 name1:我走了 name3:我走了 name2:我走了 */ $p1->say(); $p2->say(); $p3->say(); $p1=null; ?>

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

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Python's dir() function: View an object's properties and methods, specific code example required Summary: Python is a powerful and flexible programming language, and its built-in functions and tools provide developers with many convenient features. One of the very useful functions is the dir() function, which allows us to view the properties and methods of an object. This article will introduce the usage of the dir() function and demonstrate its functions and uses through specific code examples. Text: Python’s dir() function is a built-in function.

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

Bottom attribute syntax and code examples in CSS In CSS, the bottom attribute is used to specify the distance between an element and the bottom of the container. It controls the position of an element relative to the bottom of its parent element. The syntax of the bottom attribute is as follows: element{bottom:value;} where element represents the element to which the style is to be applied, and value represents the bottom value to be set. value can be a specific length value, such as pixels

Thread of Despair is a rare card in Blizzard Entertainment's masterpiece "Hearthstone" and has a chance to be obtained in the "Wizbane's Workshop" card pack. Can consume 100/400 arcane dust points to synthesize the normal/gold version. Introduction to the attributes of Hearthstone's Thread of Despair: It can be obtained in Wizbane's workshop card pack with a chance, or it can also be synthesized through arcane dust. Rarity: Rare Type: Spell Class: Death Knight Mana: 1 Effect: Gives all minions a Deathrattle: Deals 1 damage to all minions

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.
