PHP instanceof operator _php Tutorial for enforcing object types

Source: Internet
Author: User
First, Introduction

Enforcing object types in PHP, which is now a mainstream development language , can sometimes be very important. If it is missing, or because it lacks this knowledge-based on incorrect programming assumptions, or simply because of laziness, you will see results that you do not want in a particular Web application. Especially when programming with PHP(which is now the mainstream development language) 4, it is very easy to use the "is_a ()" function (although there are other methods) to verify the type of object you are using. There is no doubt that forcing object types can also be used to filter input objects (which need to be passed as parameters to other PHP in the same application(as the current mainstream development language) Class).

However, PHP(which is now the mainstream development language) 4 does not expose some of the weaknesses of its object model-in order to implement some of the features that appear in a mature object-oriented language, it may occasionally require additional code to be written. For a long time, this fact has been known as the community of PHP, which is now the mainstream development language . However, with the release of PHP(which is now the mainstream development language) 5, many of these extremely valuable features are added as part of an improved object model. They will help you more closely implement object-based code development-allowing you to use specific object features.

In the above case, you should pay special attention when it comes to object type coercion. In fact, during the execution of a Web application, PHP(as the current mainstream development language) 5 provides developers with at least two ways to examine object types--they are the "instanceof" operator and the "type hint" feature. Now go to the topic of this article, I will introduce the use of PHP(as the current mainstream development language) 5 "instanceof" operator; you will soon find that it can be very handy to determine if the object you are using belongs to a particular type.

This article will use some object-oriented examples to help you understand how to implement mandatory object types in PHP 5, whichis now a mainstream development language .

Two, you shouldn't do what

To demonstrate how to implement object type coercion in PHP(as the current mainstream development language) 5, I will use the (X) HTML widget class, as well as a simple page builder class, and make simple modifications to PHP(as the current mainstream development language) 5 development environment.

My first example enumerates some of the (X) HTML widget classes derived from an abstract base class "HtmlElement", which skips checks to their input object types. Please look at the following classes first:

Defines the abstract class Htmlelementabstract class htmlelement{protected $attributes; protected function __construct ($attributes) {if (!  Is_array ($attributes)) {throw new Exception (Invalid attribute type); } $this->attributes= $attributes; }//abstract gethtml () method abstractly protected function gethtml ();} Defines the specific class div-extension Htmlelementclass Div extends htmlelement{private $output =
  
   data= $data; The specific implementation of the//gethtml () method
  


Http://www.bkjia.com/PHPjc/508681.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508681.htmlTechArticle First, Introduction Enforcing object types in PHP, which is now a mainstream development language, can sometimes be very important. If it is absent, or because of lack of knowledge-based on not ...

The above is the PHP mandatory object type instanceof operator _php tutorial content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.