Extension and Inheritance Usage instance code for PHP classes

Source: Internet
Author: User
PHP does not have many inherited features. We rarely use this feature even if it is a programming language that supports multiple inheritance. In most people's view, multi-inheritance is not a good design method. To add an extra attribute to a class, you don't necessarily use inheritance. Here I provide a way to simulate multiple inheritance for reference. This article mainly introduces the extension and inheritance usage of PHP class, and analyzes the inheritance and extended operation skills of PHP class in detail in the instance form, and needs friends to refer to the following

<?php class Thread {var $topic;//Post topic var $body;//post content var $date;//Post release Inter-Var $author; Post author//function thread is used to initialize variables such as function thread () {//Initialize variable}//function Send to submit new post function send () {//Detect variable legitimacy After performing the insert operation, store the variable in the database}//function edit to edit post function edit () {///detect the legality of the variable after performing an update operation to store the variable in the database}//function Delete to delete post Func tion Delete () {///detects the author's permissions will remove relevant data from the database}} class Mainthread extends Thread {var $id;//Post number var $board;//Posts var $allowreply in the discussion area;     Whether to allow the reply//constructor to initialize the variable function mainthread ($id, $board, $allowreply) {//For initializing the variable} function Send () { After checking the legitimacy of a variable, perform an insert operation to store the variable in the database Parent::send (); The Send function used to call the base class} functions edit () {//to detect the legality of the variable after performing an update operation to store the variable in the database Parent::edit ();///To invoke the Edit function of the base class}} $th = New Thread; Create a new object if ($th instanceof Thread)//If the object $th is of type thread, the output is yes echo "yes"; else echo "No";? 

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.