PHP (i) OOP basics

Source: Internet
Author: User

"process-oriented & Object-oriented"
1. Process oriented: Focus on the process of solving one thing. The biggest feature is that there are functions to achieve functional requirements
2. Object-oriented: Focus on which object to achieve this function, the biggest feature, when the generation of a class with properties and methods, from the class new object, to achieve specific functions
"Object oriented"
1. What is a class?
A collection with a series of attributes (characteristics) and methods (behaviors). A class is an abstract concept.
2. What is an object?
To get an individual with a specific attribute value from a class, called an object, is a concrete instance
3. Class-To-object relationships
A class is an abstraction of an object that is the embodiment of a class.
A class is abstract: A class can only indicate that such a thing has those properties, but it cannot assign a value to a property, so the class is abstract
The object is specific: The object is generated, is the class of the individual properties of the specific assignment, the individual obtained, so the object is specific
"Declaration and instantiation of a class"
1. How to declare a class

Class class Name {        access modifier $ property Value =[=$ property value];        [access modifier] Function method name () {}}

2. Considerations for declaring a class:
①, class names can only have letters, numbers, underscores, the beginning can not be a number, must conform to the big hump law
②, class name must be decorated with class, without ()
③, property name must use Access modifier, method name can not access modifier
3. Invocation of instantiated objects and property methods
$ Object name = new class name (); () can be omitted, but generally not save
Properties and methods called outside the class
$ object Name, property name; Call property, property name must not take $
$ object name, method name;
"Constructors"
1. What is a constructor function
A constructor is a special function in a class that has the same name as a class, and is equivalent to invoking a constructor in a class when instantiating an object with the New keyword, and the constructor does not need to be called manually
2. What is the function of the constructor
When a constructor is used to instantiate an object, it is automatically called and an initialization value is assigned to a property in the class
3. How the constructor functions
①, same name as Class

[Public] function person ($name) {        $this->name = $name;}

② using the Magic method _construct, does not require the same name as the class

[Public] Function _construct ($name) {         $this->name = $name;}

4. Considerations for Constructors:
①, the first form of writing, must be the same name as the class, the case should be exactly the same
②, two construction methods write a can, while the existence of _construct effective, but will produce a strict warning
③, if there is no manual write constructor, PHP creates an empty parameter construct by default, so you can use the new person () as a non-parameter form
If you write the constructor manually, PHP will not create the null parameter construct by default, so the arguments at the time of instantiation must conform to the constructor's requirements
5. destructor _destruct
①, destructors are automatically called before objects are destroyed
②, destructors cannot have any arguments
③, destructors, and when objects are used, release resources, close resources
The Magic method in 6.PHP
PHP provides us with a series of functions that begin with __, which are called automatically at the right time, rather than being called manually by the user, and are called magic methods.
For example: constructors are called when a class is instantiated: destructors are called before an object is destroyed:
We ask!!! The custom function must not start with __

Finally, for some classes that are complex, code-intensive, and require frequent use, we typically write them in separate class files
Name the class file, unify lowercase, and use the "class name lowercase. class.php" method to name
In other files, you can use the include import if you want to use it. class.php

PHP (i) OOP basics

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.