PHP forced object type-instanceof operator _ PHP Tutorial

Source: Internet
Author: User
PHP enforces the instanceof operator for object types. I. INTRODUCTION it is sometimes important to implement mandatory object types in php (as the mainstream development language. If it is missing, or because of lack of knowledge in this area-based on introduction

It is sometimes important to implement a forced object type in php (as the mainstream development language. If it is missing, or because it lacks this knowledge-based on incorrect programming assumptions, or just because of laziness, then you will see the desired results in a specific Web application. Especially when programming with php (as the mainstream development language) 4, use the "is_a ()" function (although there are other methods) it is very easy to verify the type of the object you are using. There is no doubt that the forced object type can also be used to filter input objects (other php (as the current mainstream development language) classes that need to be passed as parameters to the same application ).

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

In the preceding case, pay special attention to object type forcing. In fact, during the execution of a Web application, php (as the mainstream development language) 5. provide developers with at least two methods to check the object type-they are the "instanceof" operator and the "type prompt" feature. Now go to the topic of this article. I will introduce the use of the "instanceof" operator in php (as the mainstream development language) 5. You will soon find that, it is very convenient to determine whether the object you are using belongs to a specific type.

This article uses some object-oriented examples to help you understand how to implement the forced object type in php (as the mainstream development language) 5.

2. what should you do?

To demonstrate how to enforce object type in php (as the mainstream development language) 5, I will use the (X) HTML widget class and a simple page generator class, and made a simple modification to fit php (as the current mainstream development language) 5 development environment.

My first example lists some (X) HTML widget classes derived from an abstract base class "HTMLElement". it skips the check of their input object types. Take a look at the following classes:

// Define 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 abstract protected function getHTML ();} // defines a specific class Div-extended HTMLElementclass Div extends HTMLElement {private $ output = <p; private $ data; public function _ construct ($ attributes = array (), $ data) {parent ::__ construct ($ attributes); $ this-> data = $ data ;} // implementation of the getHTML () method


It is sometimes important to implement mandatory object types in php (as the mainstream development language. If it is missing, or because it lacks this knowledge-based on no...

The above is the PHP forced object type of the instanceof operator _ PHP Tutorial content. For more information, please follow the PHP Chinese network (www.php1.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.