Table of Contents
CEO::__construct begin!
CEO::__construct end!
Home Backend Development PHP Tutorial 关于_set()的有关问题

关于_set()的有关问题

Jun 13, 2016 pm 01:05 PM
name property set

关于__set()的问题





PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php namespace longli;

class Employee  {
    private $name;
    public function __set($xproName, $value) {
        echo '<p>Employee::__set is called $proName=='.$xproName.', $value=='.$value.'';
        $this->xproName = $value;
    }
}
class CEO extends Employee {
    function __construct($name="") {
        echo '<h2 id="CEO-construct-begin">CEO::__construct begin!</h2>';
        $this->name = $name;
        echo '<h2 id="CEO-construct-end">CEO::__construct end!</h2>';
    }
         
}

$boss = new CEO("Blus");
?>

Copy after login




上面的代码,__set()被莫名其妙执行了两次,请问这是为什么呢? 输出结果为:

CEO::__construct begin!

Employee::__set is called $proName==name, $value==Blus

Employee::__set is called $proName==xproName, $value==Blus

CEO::__construct end!








------解决方案--------------------
__set( $property, $value ) : 给一个未定义的属性赋值时调用 
你赋值了两次当然就执行两次了。
------解决方案--------------------
第一次给未定义的类成员$name赋值: $this->name = $name; 调用一次
在调用重载方法__set的时候,第二次给未定义的类成员 $xproName赋值: $this->xproName = $value; 调用一次。
我想你的本意是这样: 
PHP code
public function __set($xproName, $value) {
        echo '<p>Employee::__set is called $proName=='.$xproName.', $value=='.$value.'</p>';
        // 这个 $xproName是一个变量,内容是变量名。 详见手册中 可变变量
        $this->$xproName = $value;
    }
<br><font color="#e78608">------解决方案--------------------</font><br>预定义了两个函数“__get()”和“__set()”来获取和赋值其属性,以及检查属性的“__isset()”<br><br><br>//__get()方法用来获取私有属性<br>private function __get($property_name)<br>{<br>if(isset($this->$property_name))<br>{<br>return($this->$property_name);<br>}else<br>{<br>return(NULL);<br>}<br>}<br>//__set()方法用来设置私有属性<br>private function__set($property_name,$value)<br>{<br>$this->$property_name=$value;<br>}
<br><font color="#e78608">------解决方案--------------------</font><br>你仍然没弄清楚__set()的作用,当你为类成员属性$xproName赋值后,它已经不再是未定义了。所以不具备死循环的条件。<br>我觉得用__set()来实现的封装是过分的封装,为什么要在类外部修改一个隐藏的属性?为什么不设为公有?<br><br>至于说 事实上,为了“添加一个隐藏属性”<br>添加一个隐藏的属性?你的程序需要的属性名难道不是固定的吗?你应该先声明该隐藏属性。<br>
<br><font color="#e78608">------解决方案--------------------</font><br>所用面向对象的语言中都对对象的每个属性设有 Access 和  Assign 方法,以便开发者灵活的控制对象的行为。由于 php 不是面向对象的语言,所以虽然粗糙的提供了 __get 和 __set 方法,但与 Access 和  Assign 相比还是有很大差距的<br>与 Access 和  Assign 一样,如果仅仅将 __get 和 __set 作用于对象属性的存取,就大有画蛇添足的嫌疑<br>  __get 和 __set 在对象中的作用是在存取属性的同时隐式的之行一些方法,而无需显式的调用方法<br> <div class="clear">
                 
              
              
        
            </div>
Copy after login
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Solution to PHP Notice: Undefined property: Solution to PHP Notice: Undefined property: Jun 22, 2023 pm 02:48 PM

When writing code in PHP, we may encounter the error message "Notice: Undefinedproperty". This error means that we are accessing an undefined property, usually because the property has not been initialized in the code. So, how to solve this problem? Here are a few possible solutions: Initialize properties This is the simplest way to solve this problem. Explicitly initializing a property in code ensures that it is defined before use. For example: class

PHP Notice: Trying to get property of non-object - Solution PHP Notice: Trying to get property of non-object - Solution Aug 17, 2023 am 09:27 AM

PHPNotice: Tryingtogetpropertyofnon-object-Solution During the PHP development process, we may encounter a common error message: Tryingtogetpropertyofnon-object (trying to get the property of a non-object). This error is usually caused when we try to access a property (or call a method) on a variable that is not an object type. This article will introduce you to this

How to delete elements from set in javascript How to delete elements from set in javascript Jan 12, 2022 am 10:56 AM

Methods to delete elements: 1. Use delete() to delete the specified element from the Set object, the syntax is "setObj.delete(value);"; 2. Use clear() to delete all elements in the Set object, the syntax is "setObj.delete(value);" "setObj.clear();".

TypeError: Cannot read property 'XXX' of null in Vue, what should I do? TypeError: Cannot read property 'XXX' of null in Vue, what should I do? Nov 25, 2023 pm 01:21 PM

Vue is a popular JavaScript framework for building user interfaces. During the development process, we may encounter various errors and exceptions. One of the common errors is "TypeError:Cannotreadproperty'XXX'ofnull". In this article, we will explore the causes of this error and how to fix it. First, let’s understand the reason behind this error. When we try to access a property or method of an object, if the pair

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Inventory of common uses of dict and set in Python programming Inventory of common uses of dict and set in Python programming Jul 25, 2023 pm 04:52 PM

This article is based on the basics of Python and introduces how to use dict and set. The dict using the key-value storage structure is very useful in Python. It is important to choose an immutable object as the key. The most commonly used key is a string.

TypeError: Cannot read property 'XXX' of undefined in Vue, what should I do? TypeError: Cannot read property 'XXX' of undefined in Vue, what should I do? Nov 25, 2023 am 10:56 AM

TypeError:Cannotreadproperty'XXX'ofundefined in Vue, what should I do? For front-end developers who use Vue to develop, they may often encounter TypeError:Cannotreadproperty'XXX'ofundefined errors during the development process. This error usually occurs when trying to access an undefined property. exist

See all articles