The instanceof controller of PHP forced object type

Source: Internet
Author: User
I. INTRODUCTION it is sometimes important to implement forced object types in PHP. If you lack it, or the lack of this knowledge-based on inaccurate programming assumptions, or just for laziness, then you will look at the specific Web application I. INTRODUCTION

It is sometimes important to implement forced object types in PHP. If you lack it, or the lack of this knowledge-based on inaccurate programming assumptions, or just for laziness, then you will see what you don't expect in a specific Web application. Especially when PHP 4 is used for programming, it is very easy to apply the 'is _ a () 'function (although there are other methods) to verify the type of the object you are applying. There is no doubt that the forced object type can also be used to filter input objects (it needs to be passed as a parameter to other PHP classes in the same exploitation program ).

However, PHP 4 does not ** expose some weaknesses in its object model-in order to implement the features presented in mature object-oriented languages, it occasionally requests to write additional code. This fact has been well known to the PHP community for a long time. However, with the release of PHP 5, many of these extremely valuable features have been added as part of the improved object model. They will help you develop object-based code more closely-promise you to apply specific object features.

In this case, pay special attention when it comes to object type forcing. In fact, during the performance of a Web application, PHP 5 provides developers with at least two methods to check the object type-they distinguish between the "instanceof" identifier and the "type prompt" feature. Now, to the topic of this article, I will first introduce the application of the 'instanceof 'controller in PHP 5. You will soon invent it, it is very convenient to determine whether the object you are applying belongs to a specific type.

This article uses some object-oriented examples to help you understand how to implement forced object types in PHP 5.

   2. what should you do?

To show how to implement object type forcing in PHP 5, I will apply the (X) HTML widget class, there is also a simple page native class, and made a brief correction to fit the PHP 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 'htmlelement'
Abstract class HTMLElement {
Protected $ attributes;
Protected function _ construct ($ attributes ){
If (! Is_array ($ attributes )){
Throw new Exception ('invalidattribute type ');
}
$ This-> attributes = $ attributes;
}
// The abstract 'gethtml () 'method
Abstract protected function getHTML ();
}
// Define a specific class 'div '-expand HTMLElement
Class Div extends HTMLElement {
Private $ output = '<div ';
Private $ data;
Public function _ construct ($ attributes = array (), $ data ){
Parent: :__ construct ($ attributes );
$ This-> data = $ data;
}
// Specific implementation of the 'gethtml () 'method
Public function getHTML (){
Foreach ($ this-> attributes as $ attribute => $ value ){
$ This-> output. = $ attribute. '= ''. $ value .''';
}
$ This-> output = substr_replace ($ this-> output, '>',-1 );
Related Article

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.