In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to create an undefined class PHP object". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to create an undefined class PHP object".
Create an undefined class PHP object
In PHP, if you don't have a class prepared in advance and you need to create an object with an undefined class, you can do this in three ways:
New stdClass ()
New class {}
(object) []
The first is stdClass, which is an empty class template and is the internal reserved keyword and class name of PHP. Think of it as the Object base class in Java, where all classes inherit naturally from the Object base class in Java. The stdClass in PHP is a completely empty class template. Your newly created class is not a subclass of it. However, with this class template, you can create an object with your own undefined class. Of course, there is nothing inside the object.
1$ a = new stdClass ()
2var_dump ($a)
What about new class {}? After working as a developer for a while, those who have been exposed to front-end js and other dynamic languages should have guessed that this is an anonymous class. It is generally common in parameter objects. It creates objects that can have property methods.
1$ b = new class {
2 public $p = 1
3}
4var_dump ($b)
The last thing we see is to use an array to generate an object in the form of an object.
1$ c = (object) [
2'p'= > 1
3]
4var_dump ($c)
Obviously, the object generated in the form of an array override is of the same type as the first object, and it can be with or without attributes. However, it can't take a way.
The object generated by the strong transformation of the array is very intuitive and easy to understand. If it is only the encapsulation of the property object, it will be more elegant and comfortable to use this way. Complex object generation can be generated using anonymous classes. For some objects that only need to occupy places, you can use the stdClass method, of course, it is also convenient to use an empty array.
It is important to note that array forcefulness requires following the rules of type conversion. For example, the problem of digital subscript.
In daily development, we often use these functions in the use of some interfaces or database ORM frameworks. For example, the inserts and modifications of some ORM frameworks need to pass in objects that contain only attributes. At this point, you can use the above method to generate objects flexibly without having to define a complete class template.
Thank you for reading, the above is "how to create an undefined class PHP object" of the content, after the study of this article, I believe you on how to create an undefined class PHP object this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.