Home > Backend Development > PHP Tutorial > Object-Oriented Programming

Object-Oriented Programming

巴扎黑
Release: 2016-11-29 09:46:35
Original
1059 people have browsed it

Arrays and objects are both composite types in php, and functions can be stored in objects.


The relationship between classes and objects

Class --> Instantiation (generating objects) --> Using objects



Class Declaration
1. What is to be developed, determine what class to write
2. The members should be related to the class
[Keywords that modify the class] class class name {
member attributes;
member methods;
}
Declare in the class Members must be preceded by modifiers (private, protected, public, static, const, var...). When you don’t know what modification to use, you can use var or public.
A file only saves one class, in the file name Contains the class name, such as file: classname.class.php

Instantiate the object through the class, and only allocate space in the memory after instantiation
1.$ variable = new class name;

Usage of the object
In the object Members must be accessed through a reference to the object
$Object-> Member attribute = value
$Object-> Member method

1. Access to members in the object (access your own member methods in the internal method of the object)
$this
$this->Variable;
$this->Method();
2. Constructor method (constructor) is the first method that is automatically called after the object is created
In a.php4, the constructor method is defined , the same method as the class name;
b.php5, the magic method __construct(), the advantage is that after changing the class name, the construction method remains unchanged;

Function: initialize member variables
Magic method: write in the class When a certain magic method is added, the functions of this method object will be added. They are all provided by the system (fixed) and are not customized. Each magic method is automatically called at different times to complete a certain function. Different magic methods have different calling timings, and they all start with __.
__construct()
__destruct()

__set()
__get()
__isset()
__unset()

__clone()
__call()
__sleep()
__weakup()
__toString()
__autoload()

Related labels:
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