PHP Force object Type instanceof operator

Source: Internet
Author: User
Tags abstract foreach define abstract object model

First, Introduction

Enforcing object types in PHP can sometimes be very important. If it is missing, or because of lack of knowledge-based on incorrect programming assumptions, or simply laziness, you will see the results you don't want in a particular Web application. Especially when you are programming with PHP 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 coercion object types can also be used to filter input objects (other PHP classes that need to be passed as arguments to the same application).

However, PHP 4 does not expose some of the weaknesses of its object model-it may occasionally require additional code to implement features that appear in a mature object-oriented language. For a long time, this fact has been known to the PHP community. However, with the release of PHP 5, many of these highly valuable features are added as part of the improved object model. They will help to implement the development of object-based code more closely-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 5 provides developers with at least two ways to examine object types-they are the "instanceof" operator and the "type hint" feature, respectively. Now go to the topic of this article, and I'll introduce the use of the "instanceof" operator in PHP 5; you'll soon find that it can be very handy to determine if the object you're using belongs to a particular type.

This article will help you understand how to implement the Coercion object type in PHP 5 with some object-oriented examples.

What you shouldn't do

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

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

Define abstract class ' HtmlElement '
Abstract class htmlelement{
protected $attributes;
protected function __construct ($attributes) {
if (!is_array ($attributes)) {
throw new Exception (' Invalid attribute type ');
}
$this->attributes= $attributes;
}
The abstract ' gethtml () ' method
Abstract protected function gethtml ();
}
Define specific class ' Div '-extended htmlelement
Class Div extends htmlelement{
Private $output = ' <div ';
Private $data;
Public function __construct ($attributes =array (), $data) {
Parent::__construct ($attributes);
$this->data= $data;
}
The concrete realization of ' gethtml () ' method
Public Function gethtml () {
foreach ($this->attributes as $attribute => $value) {
$this->output.= $attribute. ' = "'. $value." ';
}
$this->output=substr_replace ($this->output, ' > ',-1);
$this->output.= $this->data. ' </div> ';
return $this->output;
}
}
Define specific class ' Header1 '-extended htmlelement
Class Header1 extends htmlelement{
Private $output = '

Private $data;
Public function __construct ($attributes =array (), $data) {
Parent::__construct ($attributes);
$this->data= $data;
}
The concrete 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);
$this->output.= $this->data. ' return $this->output;
}
}
Define specific class ' Paragraph '-extended htmlelement
Class Paragraph extends htmlelement{
Private $output = ' <p ';
Private $data;
Public function __construct ($attributes =array (), $data) {
Parent::__construct ($attributes);
$this->data= $data;
}
The concrete realization of ' gethtml () ' method
Public Function gethtml () {
foreach ($this->attributes as $attribute => $value) {
$this->output.= $attribute. ' = "'. $value." ';
}
$this->output=substr_replace ($this->output, ' > ',-1);
$this->output.= $this->data. ' </p> ';
return $this->output;
}
}
Define specific class ' Unorderedlist '-extended htmlelement
Class Unorderedlist extends htmlelement{
Private $output = ' <ul ';
Private $items =array ();
Public function __construct ($attributes =array (), $items =array ()) {
Parent::__construct ($attributes);
if (!is_array ($items)) {
throw new Exception (' Invalid parameter for list items ');
}
$this->items= $items;
}
The concrete realization of ' gethtml () ' method
Public Function gethtml () {
foreach ($this->attributes as $attribute => $value) {
$this->output.= $attribute. ' = "'. $value." ';
}
$this->output=substr_replace ($this->output, ' > ',-1);
foreach ($this->items as $item) {
$this->output.= ' <li> '. $item. ' </li> ';
}
$this->output.= ' </ul> ';
return $this->output;
}
}

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.