Table of Contents
回复内容:
Home Backend Development PHP Tutorial 我在听XX视频课程,遇到__set()魔术方法,不能使用的问题

我在听XX视频课程,遇到__set()魔术方法,不能使用的问题

Jun 06, 2016 pm 08:21 PM
php

<code> class study{
     private  $name;
     private  $age;
     function say(){
     echo "我的名字是:".$this->name.",我的年龄是:".$this->age ;
     }
      function __set($k,$v){
              echo "1111";
 }
 }
$lisi=new study();
$lisi->age=100;</code>
Copy after login
Copy after login

我按老师视频里书写的,视频里老师的zend没有报错,而我的报错了,说age是私有属性....,难道因为视频是11年的所以,如今变化很大?还是需要什么设置?我发现我按老师说的很多魔术方法都会使用时报错,很苦恼~求助!!

回复内容:

<code> class study{
     private  $name;
     private  $age;
     function say(){
     echo "我的名字是:".$this->name.",我的年龄是:".$this->age ;
     }
      function __set($k,$v){
              echo "1111";
 }
 }
$lisi=new study();
$lisi->age=100;</code>
Copy after login
Copy after login

我按老师视频里书写的,视频里老师的zend没有报错,而我的报错了,说age是私有属性....,难道因为视频是11年的所以,如今变化很大?还是需要什么设置?我发现我按老师说的很多魔术方法都会使用时报错,很苦恼~求助!!

楼主的调用没有错啊。

假设有一个类,类中有一个私有变量$name

<code><?php class People{                                                                                   
           private $name;
           public function __set($key = '', $value = ''){
                   echo "here";
           }       
  }       
  $obj = new People();
  $obj->name = '文艺青年阿虚V';</code>
Copy after login

按照常理面向对象是不允许你这样调用的

<code>$obj->name = '文艺青年阿虚V';
</code>
Copy after login

PHP中__set的功能就是让你可以这么做,具体的流程是这样的,当PHP解析器看到你正在使用对象调用一个私有变量,这时它本想着报错呢,可是又发现你的类中有一个__set()方法,于是它就不报错,而自动执行__set()方法了


上面的__set()方法其实很简陋,只echo "here";了,既然它在上面所说的这种情况下自动调用,那我就可以在这种情况下完善一下__set()方法,让它去做你想干的事。

根据你的say()方法,我猜是你的__set方法内缺了一行
$this->$k = $v;
如果仍然有问题,确认你的php版本,__set方法是php5中有的,你不会找了一个很古老的4.x的版本吧?

然后作为属性重载方法,必须把方法的访问控制声明为public,如果你的视频教程没有提到这点,那么是一个非常不严谨的教程,我建议你赶紧换。见: http://php.net/manual/zh/language.oop5.overloading.php 当然了,由于php为了向下兼容,所以即使你不写public的访问控制,它默认也会声明称public,然而这是一个不可依赖的特性,你不知道未来PHP是否还会兼容这种写法

?太彻了 private是私有属性 只能在类里面调用

你写的没错啊,就是因为age是私有属性才会有__set()__get()

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

See all articles