PHP Object-oriented-encapsulation

Source: Internet
Author: User

First, encapsulation (using the private keyword decoration to implement the encapsulation of members)

1. Set the package

Example 3-1:

<?php

Class person{

Private $name; setting up Private members

function say () {

echo " My name is:$this->name";

}

}

$person = new Person (" Zhang San ", " male ", +);

$person, name = "John Doe"; invalid, will be error

Echo $person, name; invalid, will be error

2. Magic Method

(1) __set Magic Method

Call timing: Automatically called when assigning a value to a private member property outside the class

Parameters:2 The first parameter is the property name the second argument is the value to be assigned

Cases:

<?php

Class person{

Private $name;

function Run () {

echo "$this->name is walking ";

}

function __set ($name, $value) {

$this $name = $value;

}

}

$person = new Person;

$person, name = " John Doe "; Automatic execution of the __set () method

$person, run ();

(2) __get Magic Method

Call timing: Automatically called when a private member property is evaluated outside the class

Parameters:1 properties of the first name

To return $this property name , in the function body

Cases:

<?php

Class person{

Private $name = " Zhang San ";

Private $sex = " male ";

function __get ($name) {

return $this $name;

}

}

$person = new Person;

Echo $person-GetName ("name"); Automatic execution of the __get () method

echo $person, sex;

(3) __isset Magic Method

Call timing: automatically called when a private member property is Isset outside of the class

Parameter:1 name of the property

Returns the true and False values in the method body by judging whether the property name is set to $this .

Cases:

<?php

Class person{

Private $name = " pear orchard ";

Private $age = "100";

function __isset ($name) {

Return Isset ($this-$name);

}

}

$person = new Person;

Var_dump (Isset ($person->name)); automatically call the __isset () method

(4) __unset Magic Method

Call timing: Automatically called when you want to unset a private property outside of a class

Parameter 1 property name

unset ($this- Properties ) in the method body

Cases:

<?php

Class person{

Private $name;

Private $sex;

Private $age;

function say () {

echo " My name is:$this->name My sex is:$this->sex My age is:$this Age ";

}

function __construct ($name, $sex, $age) {

$this, name = $name;

$this, sex = $sex;

$this, age = $age;

}

function __unset ($name) {

Unset ($this, $name);

}

}

$person = new Person (" Zhang San ", " male ", +);

$person-Say ();

unset ($person, sex); automatically call the __unset () method

echo "<br/>";

$person-Say ();

3. Inheritance

Concept:

Use keyword extends

Parent Class: Inherited Class (base class)

Subclass: Inherited class (derived class)

Inheritance means that subclasses have all the properties and methods of the parent class (except the private one)

Principle:

If a subclass has a member method with the same name as the parent class, this is called rewriting (overwrite)

Private properties or Private methods in the parent class cannot be inherited by a class (subclasses cannot use private objects of the parent class)

If the subclass does not have to look for the parent class to be, if the subclass has not to look for the parent class to

If the member method is called in the parent class, the method can use the Private member property

If a method in the parent class uses a property that is private in the parent class, the property of the parent class is used instead of the child class

If you use an undeclared member property in a method, the property is automatically created as you can use it (like a variable assignment)

Cases:

<?php

1. Assigning a value to a member property that is not defined in a member method can be successful ( member properties default to Public )

Class son{

function __construct ($_n) {

$this->name=$_n;

}

function say () {

Echo $this->name;

}

}

$ztz =new Son (' Zhao Tun positive ');

$ztz->say (); output Zhao Tun positive

Echo $ztz->name; output Zhao Tun positive

2. one of the principles of inheritance is that the son is useful to his son, and the son does not use Lao

Class father{

Public $name = ' father ';

}

Class Son extends father{

function say () {

Echo $this->name;

}

}

$ztz =new Son;

$ztz->say (); output father

3. if the member property of the parent class is private, the subclass is not allowed to use the

Class father{

Private $name = ' father ';

}

Class Son extends father{

function say () {

Echo $this->name;

}

}

$ztz =new Son;

$ztz->say (); cannot output

4. if the member method is in the parent class, the property of the parent class is public, and the properties of the subclass are public, with the subclass's

Class father{

Public $name = ' father ';

function say () {

Echo $this->name;

}

}

Class Son extends father{

Public $name = ' son ';

}

$ztz =new Son;

$ztz->say (); output son

5. if the member method is in the parent class, the property of the parent class is private, with the parent class's

Class father{

Private $name = ' father ';

function say () {

Echo $this->name;

}

}

Class Son extends father{

Public $name = ' son ';

}

$ztz =new Son;

$ztz->say (); output son

Private

Protected

Public

In the same class

Class in the subclass of the

All the external members

4. Type of access control

There is a principle in the grass-roots relationship called 3 2 1 The principle is divided according to the degree of rigor

Private 3

Protected 2

Public 1

In a subclass that overloads a member property or method of a parent class, the rank can be less than or equal to the parent class

Parent: Calling a method in the parents class

Cases:

<?php

Class father{

function say () {

echo "I am the parent!" ";

}

}

Class Son extends father{

function say () {

Parent::say ();

echo " I am a sub-category";

}

}

$s =new Son;

$s->say ();

output: I am the parent Class I am a subclass

PHP Object-oriented-encapsulation

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.