PHP Object-Oriented basics

Source: Internet
Author: User
Tags access properties modifiers

This article mainly elaborates the object-oriented basic concept, if has the question, also please treatise.

First of all, some of the knowledge points involved, is an outline it.

Basic concepts of Class 1, 2 basic concepts of objects, 3 access control, 4 constructors, destructors, 5 magic methods, 6 interfaces, 7 polymorphism, 8 packages, 9 inheritance, static and final

First, the basic concept of the class

1 What is a class? A class is a collective that has the same properties and behavior. For example, the car in our daily life is a class, the car has the same properties (color, size, weight) and behavior (travel), and as animals are also a class, animals have the same attributes (size) and behavior (eat, drink); We call these things that have the same attributes and behavior as a class.

2 How to declare a class in a program, using the keyword class example

class person{    //   attributes;   //   behavior;}
  Here we affirm a human

Second, the basic concept of the object

1 What is an object? An object is either a specific thing or an abstract object that is instantiated from a class. Example: Above we say the car is a class, then BMW, Mercedes and so on is the object, summed up a sentence, the object is to the class materialized.

2 two parts of an object

(1) The constituent element of an object is the data model of the object, which is used to describe the data of the object, but also the attribute;

(2) The behavior of the object, the behavior model of the object, is used to describe what the object can do, and it is called the method;

3 Two characteristics of an object

(1) High cohesion, that is, within the object's internal properties and methods are visible internally, in the object's internal properties and methods can be modified and accessed;

(2) Low-coupling, outside of the object, we can only see a part of the method and properties, involving access control, described later.

4 How to instantiate an object, using the keyword new above we affirm a human being, and now we instantiate this human being.

Instantiate an Zhang San object

$zhangsan New Person ();

Third, access control: mainly with the modifier public protected private to control access permissions;

class person{        public       $name;  // public, can be accessed     by any protected    $sex;   // protected, can be accessed by itself and by subclasses     Private      $age;  // private and can only be accessed by themselves }

Four, constructors, and destructors

1 constructors, constructors are a method in a class that is called automatically when we instantiate the class, and the constructor is designed to do some initialization work. Using __construct Example

class person{        public       $name;  // public, can be accessed     by any protected    $sex;   // protected, can be accessed by itself and by subclasses     Private      $age;  // private, can only be accessed by itself        //When we new this class is automatically called      Public  function __construct () {       echo "Hello";}}

2 destructors, which are called automatically when an object is destroyed, we often use destructors to destroy variables to achieve the purpose of freeing up memory. Use __DESTURCT.

Five, the Magic method, the principle and the constructor function is similar, in the particular case the automatic invocation.

  1. __tostring when an object is used as a string, it is called automatically and returns a string;
  2. __invote is automatically invoked when the object is used as a method;
  3. __call when an object accesses a method that does not exist or is not exposed, it is called automatically, returning the called function name, and the parameter array;
  4. __callstatic when an object accesses a static method that does not exist, or is called automatically when it is not exposed, returns the calling function name, and the parameter array;
  5. An __get object accesses an inaccessible property when it is called automatically, returning an inaccessible property name;
  6. __set is automatically called when assigning a value to an inaccessible property, returns a property name, and an assignment parameter;
  7. __isset is automatically called when detecting an inaccessible property with Isset ()/empty (), returning the detected property name;
  8. __unset is automatically called when unset () is used to delete an inaccessible property, and the name of the deleted attribute is returned;
  9. __clone when cloning an object using the Clone keyword, it is automatically called to assign an initial value to the new cloned object;
  10. The __sleep object is serialized, automatically called, returns an array, and the values in the array are the properties that can be serialized;
  11. When the __wakeup object is deserialized, it is automatically called to deserialize the newly generated object, assigning the initial value;
  12. __autoload needs to declare the function inside and outside. When an instance of an unspecified class is invoked automatically, the class name is passed, and the corresponding class file can be loaded automatically using the class name;

VI, interface

1 What is an interface? The interface provides a set of methods that implement an interface class that must be implemented, and the methods inside the interface are abstract methods without the method body (the specific execution code) interface.

2 How to declare an interface example:

Interface Inter {    publicfunction// No specific method body public      function  run ();   }

3 When a class implements an interface, it must implement the method inside the interface, unless the class is abstract

 //declare an interfaceInterfaceInter { Publicfuntion eat ();  Publicfuntion Run ();}//a class to implement an interfaceclassPersonImplementsinter{//This class has to implement the methods in the interface.    Public  functioneat () {//the specific method}    Public functionrun () {//the specific method}}

4 interface can be inherited, with the keyword extends notation interface inter1 extends Inter {} Inter1 interface inherits the Inter interface, the inheritance of the interface can be one-to-many, that is, an interface can inherit more than one parent interface. Note: When a class implements a sub-interface, it must also implement the abstract method inside the parent interface.

Seven, polymorphic

1 polymorphism is a phenomenon, when a class is inherited by multiple subclasses, and if the methods in this class show different functions in subclasses, we call this phenomenon polymorphic.

Eight, package

The 1 package uses access modifiers to privatize properties and methods in a class that do not require external access, enabling access control so that users can access properties and methods only through the methods we provide.

The benefits of 2 encapsulation can be repeated calls to avoid the redundancy of the code.

Nine, inheriting classes can be inherited, subclasses have the properties and methods of the parent class, subclasses can modify the members of the parent class, we call it rewriting, when subclasses rewrite the methods of the parent class, run through the keyword extends to implement inheritance by the method of the subclass

Example

//declaration of a classclassperson{$name;  Public  functionsay () {Echo $this->name. " It's a nice day, huh?//$this a pseudo-variable in PHP, representing its own object. We generally use $this when accessing properties within a class }}//inherit a classclassMansextendsperson{//overrides the Say method of the parent class      Public functionsay () {Echo"What do you have for supper?" " }
}

Ten, the keyword final and static

1 final php5 keyword final when the class is modified with final, this class can be understood as the final class, which cannot be inherited; When the final modification method is used, this method can be understood as the final method and cannot be overridden.

2 static members//properties and methods with static modifiers are called static properties and static methods

(1) The static property holds the public data;

(2) Static methods can only access static properties;

(3) Static members do not have to be instantiated to access,

(4) A static method cannot invoke a non-static member, whereas a non-static method can invoke a static member because the static member has been generated at the time of loading, but not the static member at this time without an instantiation.

(5) How to access?    Inside the class access with self or static access format: self/static::$ property name; The Parent::$ property name accesses the parent class property called Outside the class: class Name:: $ property.

Conclusion: The above is a few basic concepts of object-oriented programming, and later in the busy time to get something else, saying this is under the first blog,

2018-09-02 21:39:26

PHP Object-oriented 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.