PHP object-oriented Development Learning Notes

Source: Internet
Author: User
Tags garbage collection inheritance php class

1. When defining an object method, although it is not necessary to write public by default, it is recommended to write.

PHP Object-oriented first day

1. What is Object oriented ?


Elements: Abstraction, encapsulation, sharing, emphasis on object structure rather than program structure.

What is a class?
Can be understood as a menu of feature sets, and we implement the method of generating our class.
Example: a computer
Class: Monitor/keyboard/host ... Collection (encapsulation).
Objects: monitor/keyboard/host ... One of them has its own unique function.
Properties: Computer.
Methods: Improve function, watch movies, play games, program, surf the internet ...

2. Create a class using class
There are classes in order to have a method.
Format:
Class method Name {
......
}

Member Properties: Custom variable (a name only).
Member methods: Custom Features.

3. member properties and member methods of the class
Note: Use public to define member properties do not use Var.

Instance:

The code is as follows Copy Code
Class MyPC {//declares a category that defines a method MyPC.
Public $name; member properties.
var $price; Member properties
Function VOD () {//member method to implement return string functionality.
return "Test";
}
}


4. Instantiating with new function object
Format: New object name (parameters)
Example 1:

The code is as follows Copy Code

Class MyPC {//declares a category that defines a method MyPC.
Public $name; member properties.
var $price; Member properties
Function VOD () {//member method to implement return string functionality.
return "Test";
}
}

$PC 1 = new MyPC (); Instantiation of
$PC 1-> name;


Example 2:

The code is as follows Copy Code

<?php
Class MyPC {

var $key;
Public $name;

Function VOD () {
echo "Hello";
}

}

$PC 1 = new MyPC ();
$pc 1->key = "10101010";

Echo $pc 1->key;
?>


PHP Object-oriented the next day


1. Create one and multiple objects.
2. $this keywords in the object.
3. Initialize Object __construct ()
4. destructor __destruct ()

-----------------------------------------

1. Create one and multiple objects
To create one and more objects we only need to instantiate the class multiple times using the new function.
Instance:

The code is as follows Copy Code

Class MyPC {
Public $name;
Public $price;

Function VOD () {
Return "Play movie";
}
....
}

$PC 1 = new MyPC ();
$PC 2 = new MyPC ();
$PC 3 = new MyPC ();

2. $this keywords in the object
$this keyword is a system variable used to access object properties and Object methods in the current object.
Scope: Among the classes.
Instance:

The code is as follows Copy Code
Class MyPC {
public $name;--------
Public $price; |
|
Function VOD () {|
$this->name;<--------
}
....
$this->vod (); So the output is called public $name;
....
}

Example 2:

The code is as follows Copy Code

<?php
Class MyPC {
Public $name;

Function VOD () {
Return $this->name. "Is playing the future War police";
}
function Game () {
return $this->vod (). "is running World of Warcraft";
}

}
$PC 1 = new MyPC ();
$PC 2 = new MyPC ();

$PC 1->name = "Computer No. 1th";
$PC 2->name = "Computer No. 2nd";
echo $pc 1->vod (). "<br/>";
echo $pc 2->game (). "<br/>";
?>


3. Initialize Object __construct ()
Initialization is equivalent to the initial value of a predetermined member property.

Format:
Class MyPC {
function __construct () {
Initialization operation
}
}

Instance:

  code is as follows copy code

<?p HP

Class MyPC {
 public $name;

 function __construct ($name = "") {//initialized.
   $this->name = $name;//Assign the $name = "" To the fourth row of the initialization content to the public $name of the third row taken by the $this;.
 }

The

 function VOD () {
  return $this->name. "Is playing the future War police";
 }

 function Game () {
  return $this->vod (). "is running World of Warcraft";
 }

}
$pc 1 = new MyPC ("Computer No. 1th");//This is initialized.
$pc 2 = new MyPC ("Computer No. 2nd");

Echo $pc 1->vod (). "<br/>";
Echo $pc 2->game (). "<br/>";
?

4. destructor __destruct ()
A method that can be invoked automatically when an object is released is a destructor and can be understood as a garbage collection mechanism.
Rule: LIFO, first instantiated, then released, finally instantiated, first released.
be called at last.

Instance:

The code is as follows Copy Code

<?php

Class MyPC {
Public $name;

function __construct ($name = "") {
$this->name = $name;
}

Function VOD () {
Return $this->name. "Is playing the future War police";
}

function Game () {
return $this->vod (). "is running World of Warcraft";
}

function __destruct () {//LIFO first.
echo "<br/> Garbage collection mechanism:". $this->name. "<br/>";
}

}
$PC 1 = new MyPC ("Computer No. 1th");
$PC 2 = new MyPC ("Computer No. 2nd");

echo $pc 1->vod (). "<br/>";
$PC 1 = null; This is where you can use NULL when a special need is done to reclaim memory immediately.
echo $pc 2->game (). "<br/>";
?>

PHP Object-oriented third day


encapsulation and application of classes

1. Package keywords: public, protected, private.
2. Encapsulation Correlation function: __set (), __get ().

-----------------------------------------------

1. Package keywords: public, protected, private
Encapsulation is protection and security by hiding some related properties and behaviors.

Encapsulation Key Words
Public: Represents the global, which is accessible to internal external subclasses of the class.
Protected: Represents a protected, accessible only in this class or subclass or parent class.
Private: Represents a proprietary, only within this class can be used. [Important: This keyword, when used, invokes a private property or method that can only be invoked in this class, and feels like a stepping stone. For details, see Example 2]

Public protected private
Global 1 0 0
Inheritance Class 1 1 0
This class 1 1 1

Instance:

The code is as follows Copy Code

<?php
Class MyPC {
Public $name; Public properties
protected $price; Protection properties

Private Function VOD () {//Privacy property
$this->name;
}
. . .
}
?>

Example 2:

The code is as follows Copy Code

<?php

Class MyPC {
Private $name;
function __construct ($name = "") {
return $this->name = $name;
}
Private Function open () {
Return $this->name. "---Turn on the power, is powering on";
}
Public Function ok () {
return $this->open (). "---OK";
}
}
$pc = new MyPC ("My Computer");
echo $pc->ok ();
?>


Load-related functions: __set (), __get () Action: Manipulate private properties or methods.
__set (): Gets the private or private method encapsulated in the current class, or performs a rerun or assignment operation.
Format: __set ($n, $v)

__get (): Gets (indirect access prevents raw output) the encapsulated properties or methods in the current class and converts them to public properties.

__get/__set instance:

The
  code is as follows copy code

<?php

Class MyPC {
 private $name;
 public function __construct ( $name = "") {
  return $this->name = $name;
&NBSP}

 public function __get ($name) {
  return $this->name. "__get";
 } The
 public function __set ($n, $v) {//$n the corresponding object property $name, $v corresponds to the penultimate row of __set.
   $this-> $n = $v;
 } The
 private function open () {
  return $this->name. "---Turn on the power, is powering on";
 }
 public function ok () {
  return $this->open (). "---OK";
 }
}
$pc = new MyPC ("My Computer"),
$pc->name = "__set";
Echo $pc->name;

Inheritance and application of classes

1. Inherit the keyword: extends.
2.PHP inherited rules.
3. Base class method overload and base class method access.

------------------------------------------------

1. Inheritance keyword: extends
The inheritance of the PHP class, we can understand the class capacity that shares the inherited class (base class).
Note: PHP is a single inheritance.

2. Format

The code is as follows Copy Code

Class MyPC {
...
}

Class Home extends MyPC {
...
}

3. Base class method overload and base class method access
Format: base class Name:: Original base class method name

4. Examples
Inherited:

The code is as follows Copy Code

<?php
Class Root {
Public Function Print1 () {
return "Root_print";
}
}

Class Son extends Root {
Public Function Print2 () {
return "Son_print";
}
}

$p = new Son ();
echo $p->print1 ();
?>

Overload:
If a method of a base class needs to be strengthened in a derived class, you can use the overloaded functionality

The code is as follows Copy Code

<?php

Class Root {
Public Function Print1 () {
return "Root_print";
}
}

Class Son extends Root {
Public Function Print1 () {
Return Root:: Print1 (). "<br/>son_print";
}
}

$p = new Son ();
echo $p->print1 ();
?>


Class for abstract methods and classes

In fact, it can be understood that this is a norm. Define an abstract class and method at the beginning of the class, and then inherit the abstract class in the following class, which enforces the naming of methods for the following derived classes (just the abstract method name defined in the abstract class, which can be added to itself, but cannot be modified primarily).

1. Abstract Keyword: abstract.
2. The definition of abstract methods and abstract classes.
3. Abstract classes and methods use rules.

-----------------------------------------

1. Abstract Keyword: abstract
Abstraction is not an exact description, but it has a certain concept or name.

2. Definitions of abstract methods and abstract classes
A class has at least one method that is abstract, which we call an abstract class.
So if defining an abstract class first defines an abstract method.
Format:
Abstract class Class1 {

Abstract function fun1 ();

...
}
Note: 1. There is at least one abstract method in the class. 2. The abstract method does not allow {}. 3. Abstract methods must be preceded by abstract.

3. Abstract class and method usage rules
Abstract class Features:
1. Cannot be instantiated and can only be inherited.
2. All abstract method overloads must be instantiated in an inherited derived class.
Format (cannot be instantiated):
Abstract class CL1 {

Abstract function fun1 ();

...
}

Format (can be instantiated):
Class Cl2 extends CL1 {

function fun1 () {//Overloaded abstract method
}
...
}

---------------------------------------------------
Instance:

The code is as follows Copy Code

<?php
Abstract class Chouxiang {
Abstract function fun1 ();

}

Class Paishenglei extends Chouxiang {
Public Function fun1 () {//Overload
echo "Test";
}
}

$p = new Paishenglei ();
$p->fun1 ();
?>

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.