In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to use PHP stdClass flexibly, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
PHP stdClass is not well understood for beginners of the PHP language. Next we will explain the application of PHP stdClass in detail, hoping to be helpful to you.
One-dimensional array, I especially like to write it as an object, because the keyboard is easy to play and looks pleasing to the eye, such as
$andy = array ()
$andy ['a'] = 1
$andy ['b'] = 2
$andy ['c'] = 3
I like to make it like this.
$andy = array ()
$andy = (object) $andy
$andy- > a = 1
$andy- > b = 2
$andy- > c = 3
Create a new empty array as long as $andy = array ()
Creating a new empty object requires $andy = new Andy; and requires a class Andy {} first.
Or two lines.
$andy = array ()
$andy = (object) $andy
It's a bit awkward to change the type to object directly with array. I found the answer today, thanks to phpx's brother.
Introduction of PHP stdClass
$user = new stdClass ()
$user- > name = 'gouki'
What is the use of this kind of code?
Open the manual and search stdClass, and you will find that there is almost no introduction in the manual. If you search google again, you will see almost all explanations in English.
In fact, stdClass just became popular in PHP5. PHP stdClass is also a reserved class of zend. It doesn't seem to have any other effect. There is hardly any explanation.
Or, let's put it this way: PHP stdClass is a base class of PHP, and almost all classes inherit this class, so it can be new at any time, making this variable an object. At the same time, there is a special feature of this base class, that is, there are no methods.
It is impossible to use $a-> test () whenever you use a variable called new stdClass ().
Or, we can understand that because of the uniqueness of PHP5 objects, objects are called anywhere and are address-referenced, so they consume a little less resources. When other pages assign a value to it, it is modified directly, rather than referencing a copy.
For example:
$user = new stdClass ()
$user- > name = 'gouki'
$myUser = $user
$myUser- > name = 'flypig'
In the age of PHP4, such code is consuming system resources. Because:
$myUser = $user
This is to create a copy. So, in PHP4, it's always used like this:
$myUser = & $user
Some people say, why not use arrays? Isn't it more convenient to have arrays? And for weakly typed programs like PHP, using arrays should be the most convenient.
Indeed. The array should be the most convenient in the use of the program, but every time the array is referenced ($a = $b), it actually creates a copy, and after the array is unset, it still takes up memory (according to people, I didn't test... Do not know how to test, if anyone knows, please let me know, thank you)
However, in SPL's standard class library, there is also a function arrayobject, which can directly convert arrays into objects, which is also a good way.
The above content is how to use PHP stdClass flexibly. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.