Home > Backend Development > PHP Tutorial > PHP mandatory object type instanceof operator_PHP tutorial

PHP mandatory object type instanceof operator_PHP tutorial

PHP中文网
Release: 2016-07-13 17:32:32
Original
910 people have browsed it


1. Introduction

Implementing mandatory object types in php (as the current mainstream development language) may sometimes be very important. If it's missing, either due to a lack of knowledge - based on incorrect programming assumptions, or simply laziness - then you'll see results in your particular web application that you don't expect. Especially when programming with PHP (as the current mainstream development language) 4, it is very useful to use the "is_a()" function (although there are other methods) to verify the type of the object you are using. Easy things. Of course, mandatory object types can also be used to filter input objects that need to be passed as parameters to other php(as now mainstream development languages) classes in the same application).

However, PHP (as the current mainstream development language) 4 does not expose some weaknesses about its object model - in order to implement some in a mature object-oriented language Features appear, which may occasionally require writing 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 were added as part of an improved object model. They will help implement object-based code development more closely - allowing you to use specific object characteristics. In the above case, special attention should be paid 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 methods to check object types - they are the "instanceof" operation symbols and "type hint" features. Now turning to the topic of this article, I will introduce the use of the "instanceof" operator in PHP

(as the current mainstream development language)

5; you will soon find that it can be used very conveniently Determine whether the object you are working with belongs to a specific type. This article will use some object-oriented examples to help you understand how to implement mandatory object types in php (as the current mainstream development language) 5.

2. What you shouldn’t do In order to show how to implement object type coercion in php

(as the current mainstream development language)

5, I will use ( X) HTML widget class, and a simple page builder class, which has been simply modified to suit the php

(as the current mainstream development language)

5 development environment. My first example lists some (X)HTML widget classes that are derived from an abstract base class "HTMLElement", which skips checking the type of their input objects. Please look at the following categories first:

//定义抽象类HTMLElement
abstract class HTMLElement{
 protected $attributes;
 protected function __construct($attributes){
  if(!is_array($attributes)){
   throw new Exception(Invalid attribute type);
  }
  $this->attributes=$attributes;
 }
 // 抽象的getHTML()方法
 abstract protected function getHTML();
}
//定义具体的类Div-扩展HTMLElement
class Div extends HTMLElement{
 private $output=<p ;
 private $data;
 public function __construct($attributes=array(),$data){
  parent::__construct($attributes);
  $this->data=$data;
 }
 //getHTML()方法的具体实现
Copy after login
http://www.bkjia.com/PHPjc/508681.htmlwww.bkjia.comtruehttp://www.bkjia .com/PHPjc/508681.htmlTechArticle 1. Introduction Implementing mandatory object types in PHP (as the current mainstream development language) may sometimes be very important. If it is missing, or due to lack of knowledge in this area - based on the lack of...


The above is the content of the instanceof operator of PHP mandatory object type_PHP tutorial. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template