PHP Class Problems

Source: Internet
Author: User

  
   color = $color;    }    function what_color() {      return $this->color;    }}function print_vars($obj) {   foreach (get_object_vars($obj) as $prop => $val) {     echo "\t$prop = $val\n";   

PHP new one, today in the audience see PHP object type, read a half-day puzzled
Why the result is output color = White
Can you help me annotate every line?

Reply content:


  
   color = $color;    }    function what_color() {      return $this->color;    }}function print_vars($obj) {   foreach (get_object_vars($obj) as $prop => $val) {     echo "\t$prop = $val\n";   

PHP new one, today in the audience see PHP object type, read a half-day puzzled
Why the result is output color = White
Can you help me annotate every line?

If you do not understand, then the description of the Master of the Foundation has not mastered, come, first look at the following several key points:

1, in a class, if there is the same method as the class name, the method is the construction method, but the priority is less than __construct
2, in a class, when declaring a property (or method), if no permission modifier (public, private, protected) is specified, the default is public
3,get_object_vars returns an array of properties for all public types of a class (property name = = Property value)

Now let's look at it separately:

$herbie = new Car("white");

The car method has the same name as the car class and does not declare __construct, then the car method is a construction method (such as 1), and the $this->color = $color in the method body is executed.

Now the color is equal to ' white '.

get_object_vars($obj)

Get_object_vars returns an array of attributes for all public types of a class, that is: Array (' color ' = ' white ').

The result is color = White

The What_color method in the main example is not called.

Are you sure you read the tutorial?


  
   color = $color;        // this 执行实际运行是对应的对象,上面定义了对象的属性 color,this->color 就是上面定义的那个color, 第二个 color 就是函数的参数 color    }    function what_color() {   // 对象的方法      return $this->color;     }}function print_vars($obj) {   foreach (get_object_vars($obj) as $prop => $val) { // foreach 遍历对象属性的意思     echo "\t$prop = $val\n";   }}$herbie = new Car("white");   

Come on, novice I've always recommended this:

http://www.phptherightway.com/

1 definitions of What_color are of no use
2 I think we should look at what the specific usage of get_object_vars is.
http://php.net/manual/zh/function.get-object-vars.php

You can go to IMOOC network system to learn PHP syntax and object-oriented, and then see the API Practice

It's been a long time. Constructors can be a method of the same name for a class without __construct

This code also see the egg ache, I said upstairs is very accurate

Well, this kind of constructor of the writing, for the new version, more than 5 of PHP is not recommended, but did a backward-compatible treatment, recommended to use __construct, specific can go to see the PHP manual

  • 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.