One-dimensional array, I especially like to write it as an object now, because it is easier to type on the keyboard and looks pleasing to the eye, such as
- $andy = array();
- $andy['a'] = 1;
- $andy['b '] = 2;
- $andy['c'] = 3;
I like it like this
- $andy = array();
-
$andy = (object)$ andy;
-
$andy-> = 1; $andy-
>-
b = 2; $andy-
>-
c = 3; To create a new empty array just $ andy = array();To create an empty object, you need $andy = new Andy; and you must first have a class Andy{}
or 2 lines
$
andy-
= array( ); $andy
-
= (object)$andy; It’s a bit awkward to directly change the type of array to object. I found the answer today, thanks to the brothers of phpxIntroduction to PHP stdClass
$
user- = new stdClass(); $user-
>-
name = 'gouki'; What is this code used for?
Open the manual and search for stdClass. You will find that there is almost no introduction in the manual. If you search Google again, you will see almost all English explanations.
In fact, stdClass only became popular in PHP5. And PHP stdClass is also a reserved class of zend. It seems to have no other effect. There is also almost no explanation.
Or, we can understand it this way: PHP stdClass is a base class of PHP. Almost all classes inherit this class, so it can be new at any time and this variable can become an object. At the same time, this base class has a special feature, that is, it has no methods.
Whenever a variable of new stdClass() is used, it is impossible to use $a->test().
Or, we can understand it this way. Because of the uniqueness of PHP5 objects, objects are referenced wherever they are called, so relatively fewer resources will be consumed. When assigning a value to it on other pages, it is modified directly instead of referencing a copy.
For example:
- $user = new stdClass();
-
$user-> name = 'gouki';
-
$myUser = $user;
-
$myUser-> ;name = 'flypig';
In the PHP4 era, such code would consume system resources. Because:
$myUser = $user;
This creates a copy. Therefore, in PHP4, it is always used like this:
$myUser = & $user;
Some people say, why not use an array? Wouldn't arrays be more convenient? And for weakly typed programs like PHP, using arrays should be the most convenient.
Indeed. Arrays should be the most convenient to use in programs. However, every time the array is referenced ($a = $b), a copy is actually created. Moreover, after the array is unset, it still occupies memory (this is what I heard) People say it, I haven’t tested it... and I don’t know how to test it. If anyone knows, please tell me, thank you)
However, there is also a function arrayobject in the standard class library of SPL, which can directly convert an array into an object. This is also a good idea.
http://www.bkjia.com/PHPjc/446056.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446056.htmlTechArticleOne-dimensional array, I especially like to write it as an object now, because it is easier to type on the keyboard and looks pleasing to the eye. , such as $andy = array (); $andy['a']=1; $andy['b']=2; $andy['c']=3; I like to put...