Table of Contents
PHP object directly accesses private properties
Home Backend Development PHP Tutorial PHP object directly accesses private properties_PHP tutorial

PHP object directly accesses private properties_PHP tutorial

Jul 12, 2016 am 09:07 AM
object Attributes

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1061538.htmlTechArticlephp object directly accesses private properties?php header(content-type:text/html;charset=UTF-8) ; class Person{ //Private member attributes, for direct access like private $name; private $age; private...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert an array or object to a JSON string using PHP's json_encode() function Convert an array or object to a JSON string using PHP's json_encode() function Nov 03, 2023 pm 03:30 PM

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 the properties and methods of an object Python's dir() function: View the properties and methods of an object Nov 18, 2023 pm 01:45 PM

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.

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

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.

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

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.

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

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 in CSS bottom attribute syntax in CSS Feb 21, 2024 pm 03:30 PM

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

Introduction to the attributes of Hearthstone's Despair Thread Introduction to the attributes of Hearthstone's Despair Thread Mar 20, 2024 pm 10:36 PM

Thread of Despair is a rare card in Blizzard Entertainment's masterpiece &quot;Hearthstone&quot; and has a chance to be obtained in the &quot;Wizbane's Workshop&quot; 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

What should I pay attention to when a C++ function returns an object? What should I pay attention to when a C++ function returns an object? Apr 19, 2024 pm 12:15 PM

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.

See all articles