PHP Object-oriented learning

Source: Internet
Author: User

PHP Object-oriented learning

Recently with others to develop a set of PHP program, deeply oneself object-oriented is very idiot, so once again consolidated a bit of object-oriented learning, self-organized a bit of these days object-oriented things, to everyone share!!

Three main features of object-oriented:

Encapsulation--hide internal implementations, stabilize external interfaces

Inheritance--subclass inherits parent class member, implements code reuse

Polymorphic-different subclasses react differently to the same message

First, the interface-is a set of specifications, compliance with this specification can be realized function

In PHP, interfaces are also a specification and standard that can constrain the behavior of a class and define an interface that does not specify a specific implementation.

An interface is the combination of implicit public methods and properties to encapsulate a collection of specific functions. Once an interface is defined, it can be implemented in a class. In this way, the class can support all the properties and members specified by the interface.

Note: interfaces cannot exist alone. An interface cannot instantiate an interface as if it were instantiating a class. An interface cannot contain any code that implements its members, but can only define the member itself. Implementing an interface must be implemented in a class that references an interface.

A class can support multiple interfaces, and multiple classes can also support the same interface. So the concept of interfaces makes it easier for users and other developers to understand other people's code.

Ii. polymorphism

1, refers to different objects receive the same message, will produce different behavior 2, the same class in different situations show different behavior characteristics

Iii. abstract classes and abstract methods

1, abstract class is used to enumerate the behavior required by a Class 2, the abstract class does not explicitly provide a concrete implementation method 3, the abstract class must be implemented by its subclasses of its abstract method (unless the subclass is also abstract) 4, the abstract class cannot be instantiated 5, abstract class cannot be locked (final decoration)

Iv. Application of abstract classes

1, abstract class and abstract method to achieve polymorphism 2, the parent class provides a series of provisions, the behavior of the constraints subclass 3, the parent class can provide some common behavior above is my summary of object-oriented, the following is the details of the focus of the PHP object-oriented, difficult! Class: [url=http://www.t00ls.net/#viewSource] Link Tag preview source code [/url] [url=http://www.t00ls.net/#printSource] link marker print [/url][url =http://www.t00ls.net/#about] Link tag about [/url]01<?php

02/*

Basic knowledge of Class 03

04 1, class uses the class keyword definition, the name of the class is capitalized in the first letter of each word

05 2, class attributes must be decorated with encapsulated keywords (public, protected, private)

06 3, class methods can not be decorated by the Encapsulation keyword, the default is: public

07 4, constructor in class: __construct () {}, default is null, Initialize property.

08 constructors can also be overloaded, with any number of arguments. Note: The constructor in PHP

09 can only have one.

10 5, Destructor in class: __destruct () {}

11*/

12/*

Package keywords for class 13

14 1, public--open

15 2, protected--protected

16 3, private--private

17

18 is because of the use of protected or private encapsulation keywords, so the creation of a read-write method,

19 The method of writing can verify the legality of the data, and the method of reading can be protected or private

20 sexual protection is not externally modified.

21st

22 4, __get ($n) {return $this, $n;} Reading method

23 5, __set ($n, $v) {$this, $n = $v;} Writing method

24

25*/

26class people{

Private $name;

$age private;

Private $sex;

30

31//Constructor

function __construct ($name, $age =18, $sex = "male") {

$this, name = $name;

$this-age = $age;

$this, sex = $sex;

echo $this Show ();

37}

38

39//destructor

function __destruct () {

//echo "{$this->name} was released! ";

42}

43

44//Read properties

function __get ($n) {

if ($n = = "Name") {

$n $this.

48}

49}

50

51//Write Property

__set function ($n, $v) {

if ($n = = "Name") {

$this-$n = $v;

55}

56}

57

The Public Function show () {

"Congratulations!" Create {$this->name} object successfully! ";

60}

61}

62

63/*

64 inheritance

65 1, use keyword extends

66 2, the inherited class is called: Parent class (base class)

67 3. The inheriting class is called: Subclass (Derived class)

68 4, single inheritance

69 5, has transitivity, namely: The parent class has, the subclass also will have

70 6, the wrapper keyword level of the subclass cannot be lower than the parent class

71 7, the parent class constructor is also inherited

72 8, heavy-duty

73*/

74class Stu extends people{

75

76

The Public Function show () {

Parent::show return (). " O (∩_∩) o haha ~ ";

79}

80}

81$stu = new Stu ("Zhang San");

82echo $stu, name;

83?> abstract class: [url=http://www.t00ls.net/#viewSource] Link Tag preview source code [/url] [url=http://www.t00ls.net/#printSource] link marker print [ /url][url=http://www.t00ls.net/#about] Link tag about [/url]01<?php

02/*

03 Abstract class

04 1. Classes that use the abstract keyword are called abstract classes, and there is at least one abstract method in the class.

05 There are specific ways to do this.

06 2, abstract class can not be instantiated, cannot be locked (final decoration), can only be inherited

07 3, an abstract class must implement its abstract method by its subclasses (unless the subclass is also abstract)

08*/

09abstract class animal{

Ten protected $name;

One by one protected $age;

protected $weight;

13

Abstract function __construct ();

15

Abstract function Eat ($name);

17

Abstract function sleep ();

19

static function Show ($what) {

Return "I am {$what->name}! ";

22}

23}

24

25//dog class

26class Dog extends animal{

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

$this, name = $name;

$this-age = $age;

$this-weight = $weight;

31}

32

Eat function ($n) {

->name return $this. " In eating ". $n;

35}

36

PNS function sleep () {

->name return $this. " Sleep in the right! ";

39}

40

Wangwang function () {

"Barking!" ";

43}

44}

45

46//cat class

47class Cat extends animal{

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

$this, name = $name;

$this-age = $age;

Wuyi $this-weight = $weight;

echo $this->miaomiao ();

53}

54

Eat function ($n) {

->name return $this. " In eating ". $n;

57}

58

The function sleep () {

return $this->name. " Sleep in the right! ";

61}

62

Miaomiao function () {

"Mew return" Meow Meow!!! ";

65}

66}

67

68$dog = new Dog ("Wang Choi", 4, "10kg");

69echo $dog->sleep ();

70echo "";

71$cat = new Cat ("Dragon Cat", 2, "5kg");

72echo "";

73echo $cat->sleep ();

74echo "";

75echo animal::show ($dog);

76echo "";

77echo animal::show ($CAT);

78?> interface: [url=http://www.t00ls.net/#viewSource] Link Tag preview source code [/url] [url=http://www.t00ls.net/#printSource] link mark Print [/ url][url=http://www.t00ls.net/#about] Link tag about [/url]01<?php

02interface iusb{

name1= Const "3.0 interface";

type1 function ($what);

function Power1 ();

06}

07

08interface iapi{

name2= Const "Expansion slot";

Ten function type2 ();

One function power2 ();

12}

13

14class Pc implements iusb,iapi{

15

function Type1 ($what) {

if ($what = = iusb::name1) {

Return iusb::name1. $this->power1 ();

19}

else{

"The interface is not correct, cannot use!" ";

22}

23}

24

function Power1 () {

"The interface is correct, the power is on ...";

27}

28

function type2 () {}

30

-function Power2 () {}

32}

33

34class MP3 implements iusb{

Public $name = iusb::name1;

function Type1 ($s) {}

PNS function Power1 () {}

38}

39$p = new Pc ();

40$mp3 = new Mp3 ();

41echo $p-type1 ($mp 3->name);

PHP Object-oriented learning

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.