An understanding of creating entity classes using PHP

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Recently saw students asking such questions (reproduced)</strong></p></p><p><p>1. How PHP implements Java-like set and get</p></p><p><p>2. How PHP constructs entity classes</p></p><p><p></p></p><p><p><strong>A lot of people have asked before, so here's a quick answer.</strong></p></p><p><p></p></p><p><p><strong>3 words:</strong></p></p><p><p>1, about the role of the use of entity classes in Java Or. net, the benefits and the cost of forcing people to do their own baidu, not to Repeat.</p></p><p><p>2, PHP If the business is not very complex (such as complex to you can not remember how many business data tables), generally do not need to establish a physical class</p></p><p><p>3, do not study in PHP what the generic flow of things.  If this is hard, why not use Java directly? PHP is for the fast-looking cool to do web,php if one day changed to java-like syntax structure, or even must be compiled to Run. Then I would definitely abandon PHP and use Java directly</p></p><p><p></p></p><p><p><strong>So Here's the Problem. For example, I like to use the same entity class as java, what should I do?</strong></p></p><p><p>Such as</p></p><p><p>function Getnews ()</p></p><p><p>{</p></p><p><p>Here I'm going to return a collection of news entity Classes.</p></p><p><p>For example $news =new newsmapper ();</p></p><p><p>$news->title= "xxx";</p></p><p><p>$news->id=xxx;</p></p><p><p>Return array ($news);</p></p><p><p>}</p></p><p><p></p></p><p><p>To this Code. Many people will be affected by "java Or. net", especially to create a class externally called newsmapper, for example:</p></p><p><p>Class Newsmapper</p></p><p><p>{</p></p><p><p>Public $title = "";</p></p><p><p>Public $id = 0;</p></p><p><p>}</p></p><p><p>of course, <strong><span style="color: #e53333;">It is possible</span></strong> to do So. No problem, then you just have to include or require right,</p></p><p><p></p></p><p><p>I said in the title "misunderstanding", not that the above is wrong to write the Wording. But some small partners think it is <strong><span style="color: #e53333;">necessary to write this</span></strong> . ( <strong><span style="color: #e53333;">This is the misunderstanding</span></strong> )</p></p><p><p></p></p><p><p><strong>so, look, What should we do if we don't use the above notation?</strong></p></p><p><p></p></p><p><p>function Getnews ()//or This method</p></p><p><p>{</p></p><p><p>Here I'm going to return a collection of news entity Classes.</p></p><p><p>Such as</p></p><p><p>$news->title= "xxx";</p></p><p><p>$news->id=xxx;</p></p><p><p>Return array ($news);</p></p><p><p>}</p></p><p><p><strong>Yes. If you read it right, you can write it straight.</strong></p></p><p><p>however, the runtime will report a warning telling you that the $news object does not have a defined flow. In fact, you can close this warning directly in php.ini</p></p><p><p></p></p><p><p>of course, to "spec," you just don't want to dismiss the Warning.</p></p><p><p>function Getnews ()//or My Ladyship</p></p><p><p>{</p></p><p><p>Here I'm going to return a collection of news entity Classes.</p></p><p><p>For example $news =new stdClass (); <strong> <span style="color: #e53333;">//note here</span></strong></p></p><p><p>$news->title= "xxx";</p></p><p><p>$news->id=xxx;</p></p><p><p>Return array ($news);</p></p><p><p>}</p></p><p><p>Yes. You are right, that's it. Stdclass is the Top-most class of PHP and does not include any properties and METHODS. is equal to a naked class without clothes.</p></p><p><p></p></p><p><p>When you execute $news->title. PHP will automatically help you add Properties. And so on</p></p><p><p></p></p><p><p>This practice is typically used to return the JSON format for the foreground Call. At the same time you do not need to know the class in other places, do not need to know the class Name. Only the properties of this class need to be Called.</p></p><p><p>also, if you're just using it temporarily, just like PHP7 's anonymous class. Then this method should be Used. Instead of "just 1-2 attributes" also to build an entity class. (empty space?) )</p></p><p><p></p></p><p><p><span style="background-color: #ff0000;">If you use classes to pass data from the front to the logical and data tiers, you need the first entity class to be Written.</span></p></p><p><p>An understanding of creating entity classes using PHP</p></p></span>

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.