PHP Learning Notes

Source: Internet
Author: User

The addition of class dynamic properties in PHP

From Stdclass, finding this class has the following definition

Stdclass began to be popular in PHP5. And Stdclass is also a reserved class for Zend. The Stdclass class is an internal reserved class for PHP, with no member variables and no member methods at the beginning, and all magic methods are set to NULL. The use of $a->test () is unlikely to occur in any variable with new StdClass (). The uniqueness of PHP5 objects, where objects are invoked anywhere, are referred to as address types, so the relative consumption of resources will be less. When the other page assigns a value to it, it is modified directly instead of referencing a copy.

But I see the following code:

$CFG = new StdClass ();

$CFG->dbtype = ' mysqli ';
$CFG->dblibrary = ' native ';
$CFG->dbhost = ' localhost ';
$CFG->dbname = ' moodle26 ';
$CFG->dbuser = ' Raoqiang ';
$CFG->dbpass = ' Wodemima ';
$CFG->prefix = ' mdl_ ';
$CFG->dboptions = Array (
' Dbpersist ' = 0,
' Dbsocket ' = 0,
);

Check out the following and know that the same problem is the case

<?php
Class foo{

}
$foo = new Foo ();
Var_dump ($foo);
$foo->test = ' aaaaaaaa ';
Var_dump ($foo);
$foo->show = ' bbbbbbbbbbbb ';
Var_dump ($foo);


///////////////////////////////////////////////

This is the kind of thing, the class itself does not define the variable but can be instantiated again when the dynamic to add it, it feels very strange ah

Cannot understand why this class without any member variables and member methods can assign arbitrary values to member variables, and as if these member variables were not defined at the beginning, then I'll look it up and check

Finally, it's a way to add properties dynamically.

The PHP manual is also called the overload method.

Http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

Overloading¶

Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities is processed via magic methods one can establish in a class for various action types.

The overloading methods is invoked when interacting with properties or methods that has not been declared or is not vis Ible in the current scope. The rest of this section would use the terms 'inaccessible properties ' and 'inaccessible methods ' to refer Combination of declaration and visibility.

All overloading methods must is defined as public.

Note:

None of the arguments of these magic methods can be passed by reference.

Note:

PHP ' s interpretation of "overloading" is different than most object oriented languages. Overloading traditionally provides the ability to has multiple methods with the same name but different quantities and Ty pes of arguments.

Changelog¶

The discussion above could describe the problem clearly, but it did not seem to explain it, so someone gave an explanation of the overload in the handbook above.

This explanation means that this class can add attributes to the dynamic by a thing called magic methods, and I think I've found some magical function.

PHP Learning Notes

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.