PHP learning notes-Object-oriented design

Source: Internet
Author: User
Tags inheritance lowercase

Simple and modular maintenance is a feature of object-oriented programming. Objects are represented as classes with the same functions in the same namespace. We can add a class in the namespace without affecting other members of the namespace.
Extensibility object-oriented programming essentially supports extensibility. If a class has a certain function, you can quickly expand the class and create a class with the extended function.
Code reuse because the function is encapsulated in the class and the class exists as an independent entity, it is very easy to provide a class library.
It is suitable for many people to work together to develop projects, so many large and medium-sized websites now choose to use OOP for development.

This article mainly explains the most basic methods and code instances for object-oriented programming using php, how to create a class and how to generate a class instance, etc. It is just an entry-level and very simple, it is far from enough. Only suitable for beginners of php

Public indicates global and can be accessed by external subclass inside the class;

The code is as follows: Copy code
<? Php
    
Class Test {
Public $ name = 'janking ',
$ Sex = 'male ',
$ Age = 23;
        
Function _ construct (){
Echo $ this-> age. '<br/>'. $ this-> name. '<br/>'. $ this-> sex. '<br/> ';
         }
        
Function func (){
Echo $ this-> age. '<br/>'. $ this-> name. '<br/>'. $ this-> sex. '<br/> ';
         }
     }
 
 
$ P = new Test ();
Echo '<br/> ';
$ P-> age = 100;
$ P-> name = "Rainy ";
$ P-> sex = "female ";
$ P-> func ();
?>


Private indicates private, which can only be used inside the class;

The code is as follows: Copy code


<? Php
    
Class Test {
Private $ name = 'janking ',
$ Sex = 'male ',
$ Age = 23;
        
Function _ construct (){
$ This-> funcOne ();
         }
        
Function func (){
Echo $ this-> age. '<br/>'. $ this-> name. '<br/>'. $ this-> sex. '<br/> ';
         }
        
Private function funcOne (){
Echo $ this-> age. '<br/>'. $ this-> name. '<br/>'. $ this-> sex. '<br/> ';
         }
     }
 
 
$ P = new Test ();
Echo '<br/> ';
$ P-> func ();
$ P-> age = 100; // Cannot access private property Test: $ age
$ P-> name = "Rainy"; // Cannot access private property Test: $ name
$ P-> sex = "female"; // Cannot access private property Test: $ female
$ P-> funcOne (); // Call to private method Test: funcOne () from context''
?>


Protected indicates that it is protected and can be accessed only in this class or subclass or parent class;

-Data abstraction and information hiding
-Inheritance
-Polymorphism

How to encapsulate classes in PHP:

The code is as follows: Copy code

Class Something {
// In OOP classes are usually named starting with a cap letter.
Var $ x;

Function setX ($ v ){
// Methods start in lowercase then use lowercase to seprate
// Words in the method name example getValueOfArea ()
$ This-> x = $ v;
}

Function getX (){
Return $ this-> x;
}
}

?>

Of course you can use your own method, but there is always a standard.

The data member of the class in PHP is defined by "var". The data member is of no type until it is assigned a value. A data member may be an integer, array, associative array, or even an object ). A method is defined as a function in a class. To access data members in a method, you must use $ this-> name. Otherwise, a method is a local variable of a function.

Use new to create an object

The code is as follows: Copy code

$ Obj = new Something;

Then use the member function

The code is as follows: Copy code

$ Obj-> setX (5 );
$ See = $ obj-> getX ();

The setX member function assigns 5 to the member variables in the object (rather than the class) obj, and then returns the value of getX 5.

You can also use object references to access member variables, such as $ obj-> x = 6. However, this is not a good object-oriented programming method. I insist that you should use the member function to set the value of the member variable and read the member variable through the member function. If you think that member variables are unaccessable, in addition to using member functions, you will become a good object-oriented programmer. Unfortunately, PHP itself cannot declare that a variable is private, so bad code is allowed.

In PHP, extend is used for inheritance.

The code is as follows: Copy code

Class Another extends Something {
Var $ y;
Function setY ($ v ){
// Methods start in lowercase then use lowercase to seperate
// Words in the method name example getValueOfArea ()
$ This-> y = $ v;
}

Function getY (){
Return $ this-> y;
}
}

?>

In this way, the object of the "Another" class has the member variables and method functions used by the parent class, plus its own member variables and member functions. For example:

The code is as follows: Copy code

$ Obj2 = new Another;
$ Obj2-> setX (6 );
$ Obj2-> setY (7 );

Multi-inheritance is not supported, so you cannot let a class inherit multiple classes.

You can redefine the method in the inheritance class. If we redefine getX in "Another", we will no longer be able to access the member function getX in "Something. similarly, if we declare a member variable with the same name as the parent class in the inherited class, the variable that inherits the class will hide the variable with the same name as the parent class.

You can define a class constructor. The constructor is a member function with the same name as the class and is called when you create class objects.

The code is as follows: Copy code

Class Something {
Var $ x;

Function Something ($ y ){
$ This-> x = $ y;
}

Function setX ($ v ){
$ This-> x = $ v;
}

Function getX (){
Return $ this-> x;
}
}

?>

You can use the following method to create an object:

The code is as follows: Copy code

$ Obj = new Something (6 );

The constructor automatically assigns 5 values to the member variable x. The constructor and member functions are common PHP functions, so you can use the default parameters.

The code is as follows: Copy code

Function Something ($ x = "3", $ y = "5 ")

Then:

The code is as follows: Copy code

$ Obj = new Something (); // x = 3 and y = 5
$ Obj = new Something (8); // x = 8 and y = 5
$ Obj = new Something (8, 9); // x = 8 and y = 9

The definition method of the default parameter is the same as that of C ++. Therefore, you cannot pass a value to Y but let X take the default value. The real parameter is passed from left to right, if no more arguments are available, the function uses the default parameters.

Only when the constructor of the inherited class is called Can the object of the inherited class be created, and the constructor of the parent class is not called. This is a feature of different PHP object-oriented languages, because the constructor call chain is a feature of object-oriented programming. If you want to call the base class constructor, you have to explicitly call it in the constructor of the inherited class. This method works because all methods of the parent class are available in the inherited class.

The code is as follows: Copy code

Function Another (){
$ This-> y = 5;
$ This-> Something (); // explicit call to base class constructor.
}

?>

Polymorphism.

The code is as follows: Copy code

Function niceDrawing ($ x ){
// Supose this is a method of the class Board.
$ X-> draw ();
}

$ Obj = new Circle (3,187 );
$ Obj2 = new Rectangle (4, 5 );

$ Board-> niceDrawing ($ obj); // will call the draw method of Circle.
$ Board-> niceDrawing ($ obj2); // will call the draw method of Rectangle.

?>


Encapsulation-related magic methods:

_ Set (): The method automatically called when the property value of a private member is directly set.
_ Get (): The method automatically called when the private member attribute value is directly obtained.
_ Isset (); this method is automatically called when the isset directly checks whether the private attributes in the object are stored.
_ Unset (); is the method automatically called When unset directly deletes the private attribute of an object.


In general, although there are more than seven habits of building OO software, following these seven habits can make the code conform to the basic OO design standards. They will provide you with a stronger foundation on which to build more OO habits and build software that can be easily maintained and expanded. These habits target several main features of modularity. For more information about language-independent OO design advantages, see references.

Seven excellent php oo habits include:

◆ Be humble.
◆ Be a good neighbor.
◆ Avoid seeing Medusa.
◆ Use the weakest link.
◆ You are an eraser; I am a glue.
◆ Restricted propagation.
◆ Use mode.

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.