I. Creating Classes and objects
User-defined classes are also one of the prerequisites for learning PHP well. Classes in PHP are relatively simple compared to classes in other object-oriented languages. PHP has only classes (class), methods, attributes, and single inheritance (extensions). In PHP, to create a class is very simple, just need the keyword class, one of the simplest class is defined as follows:
Class class_name{//defines the classes ' properties with the keyword Var, the type of the property supports all PHP data types, or an object var $var _1;var $var _2;...var $var _n;// To define a class's method function Method_1 (...) with the keyword function. {...} function Method_2 (...) {...} ... function method_n (...) {...}}
Where class is the keyword that declares the classes must use, Class_name represents the class name, the choice of the class name should have some meaning, so as to facilitate memory and understanding; {} is used to identify the beginning and end of a class.
Note: If you use a property or method of the class itself in an implementation inside a class, you need to precede the property or method with "$this" to indicate that you are using its own member, and that the "$" tag is no longer used before the property.
Create a Class object with the keyword new in PHP with the following syntax:
$object _name=new class_name
Where object_name is the name of the object to be created, the keyword new is used to create an object, Class_name the class name.
Second, using the class encapsulation code makes its invocation more convenient, the code integration is higher, the extensibility is better, the maintainability is stronger. This example is primarily applied to classes, including class declarations, class bodies, and invocation classes. Write a class of library information and call it using PHP. The class should be written in a PHP page when writing the class, so that the definition and method of the class are located in a php page, when the class is modified, do not need to recompile, only need to rerun the PHP page code.
(1) write a book class in the class_book.php file.
Use class to indicate that it is a class category. The book in the function in the category represents a method of the class. The method encapsulates the actual processing of the class, allowing the class to perform some steps independently of the encapsulated method.
<?phpclass book{ //Declaration class Var $book _id; Book number var $book _name; Book name var $book _author; author Var $book _tpi; Publishing house var $book _price; Pricing function Book ($ID, $Name, $Author, $TPI, $Price) {$this->book_id= $ID; $this->book_name= $Name; $this->book _author= $Author; $this->book_tpi= $TPI $this->book_price= $Price;}}? >
The $this class variable in the program belongs to a special variable in PHP, $this variable is used only in class categories to represent the class itself.
(2) Call the class method. Save the above defined class as a PHP file, and then use require (or include) to include it. To define the variable $obj, use the new reserved word, and use the "-a" symbol to represent the method that executes the class.
<?php include "class_book.php"? ><?php$obj=new book ("7-115-14688-8", "PHP Database Development Example Tutorial", "Friday", "Changjiang Press", (); echo "book Number: $obj->book_id<br>"; echo "book Name: $obj->book_name<br>"; echo "Author: $obj->book_author<br>"; echo "Publisher: $obj->book_tpi<br>"; echo "Pricing: $obj->book_price< Br> ";?