I am listening to XX video courses and have encountered the _ set () magic method, which cannot be used.

Source: Internet
Author: User
{Code ...} I wrote it in the instructor's video. The instructor's zend in the video did not report an error, but I reported an error saying that age is a private property ...., is it because video has been around for 11 years and is changing a lot today? Or what settings are required? I found that I reported an error when using many magic methods as the teacher said ,...
Class study {private $ name; private $ age; function say () {echo "My name is :". $ this-> name. ", my age is :". $ this-> age;} function _ set ($ k, $ v) {echo "1111" ;}}$ lisi = new study (); $ lisi-> age = 100;

I wrote it in the instructor's video. The instructor's zend in the video did not report an error, but I reported an error saying that age is a private property ...., is it because video has been around for 11 years and is changing a lot today? Or what settings are required? I found that I reported an error when using many magic methods according to my instructor ~ Help !!

Reply content:
Class study {private $ name; private $ age; function say () {echo "My name is :". $ this-> name. ", my age is :". $ this-> age;} function _ set ($ k, $ v) {echo "1111" ;}}$ lisi = new study (); $ lisi-> age = 100;

I wrote it in the instructor's video. The instructor's zend in the video did not report an error, but I reported an error saying that age is a private property ...., is it because video has been around for 11 years and is changing a lot today? Or what settings are required? I found that I reported an error when using many magic methods according to my instructor ~ Help !!

The landlord's call is correct.

Assume there is a class with a private variable $ name in the class.


  Name = 'literary youth a virtual V ';

Generally, object orientation does not allow you to call this method.

$ Obj-> name = 'literary youth a virtual V ';

In PHP, the _ set function allows you to do this. The specific process is as follows. When the PHP parser shows that you are using an object to call a private variable, at this time, it was intended to report an error, but it found that your class has a _ set () method, so it does not report an error.Auto execute _ set ()Method

The above _ set () method is actually very simple, only echo "here"; now, since it is automatically called in the above case, then I can improve the _ set () method in this case to let it do what you want.

According to your say () method, I guess a row is missing in your _ set method.
$this->$k = $v;
If the problem persists, check your php version,__setThe method is in php5. You won't find a very old version of 4.x, right?

As an attribute overload method, you must declare the access control of the method as public. If this is not mentioned in your video tutorial, it is a very rigorous tutorial. I suggest you change it quickly. See: http://php.net/manual/zh/language.oop5.overloading.php of course, because php for backward compatibility, so even if you do not write public access control, it will be declared by default public, but this is a non-dependent feature, you don't know If PHP will be compatible with this method in the future

? Private is a private property that can only be called in the class.

You did. That's because age is a private property.__set()And__get()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.