PHP Object-Oriented Programming learning Note _php Tutorial

Source: Internet
Author: User
Object-oriented programming is a commonly used method in PHP, this article introduces the object-oriented simple use of PHP and some basic knowledge of the needs of friends to enter the reference.

(OOP) to develop. Object-oriented development has many advantages over process-oriented:
Maintaining a simple modularity is a feature in object-oriented programming. Entities are represented as classes and classes with the same functionality in the same namespace, we can add a class in the namespace without affecting other members of that namespace.
Extensible object-oriented programming essentially supports extensibility. If you have a class with some kind of functionality, you can quickly expand the class to create a class with expanded functionality.
Code reuse because functionality is encapsulated in a class, and the class exists as an independent entity, providing a class library is straightforward.
It is more suitable for multi-person cooperation to develop projects, so now many large and medium-sized sites have chosen to use OOP to develop.

Let me introduce you to object-oriented programming.

Classes and properties and methods

The class syntax format is defined in PHP:

The code is as follows Copy Code
class classname [optional attribute]{
Public $property [=value]; ...//Declare a common identity with a, and then give a variable variable can also assign a value
function functionname (args) {//member functions in Methods of class
Code} ...
Method of the Class (member function)
}

Build object (Instantiation of Class): $ object Name =new classname ();
Using the properties of an object

In a class, you can access a special pointer $this when you set or access the variable by an action in the class, use $this->name to refer to it.

The code is as follows Copy Code

Class person{
function _ _destruct ()
{echo "Bye bye!";}
}
$a =new person ();


1.final
FINAL:PHP5 adds a final keyword. If a method in the parent class is declared final, the subclass cannot overwrite the method, and if a class is declared final, it cannot be inherited.

The code is as follows Copy Code

Class baseclass{
Public Function test () {
Ehco "Test";
}

Final public Function moretest () {
echo "Moretest";
}
}

Class ChildClass extends baseclass{
Public Function moretest () {
echo "Moretest";
}
}
Generate Fatal Error:cannot Override final Method Baseclass::moretest ()


2.__tostring (recommended with PHP5.2 or later)

The code is as follows Copy Code
Class person{
protected $name;
protected $email;

Public Function SetName ($name) {
$this->name = $name;
}

Public Function Setemail ($email) {
$this->email = $email;
}

Public Function __tostring () {
Return "$this->name < $this->email>";
}
}
$rasums = new Person;
$rasums->setname (' Test ');
$rasums->setemail (' test@qq.com ');
Print $rasums;



3. Interfaces and abstract classes

The role of an interface: you want to ensure that a class implements one or more methods according to a particular name, visibility, and prototype.
Requirements for the interface:
All abstract methods in a class
Abstract method Money does not add abstract
Interface Abstract Method property is public
The member property must be a constant
Cases:

The code is as follows Copy Code
Interface childtest{
Public function childtest ();
}
Class Fathtest implements childtest1,childtest2{
Public Function childtest () {
Echo 1;
}
............
}


Abstract role: In fact, the abstract class and interface classes have a very similar, remember where to see such a sentence, the abstract class like the part of the extraction, this looks very funny, in fact, it said the abstract class of truth, the role of abstract class, when you find many of your classes in many ways you constantly in the repeated writing, Then you can consider using the abstract class, you might say "I can not rewrite a class each public class I instantiate a public class, call the same method can be," Here is OK, actually the abstract class does the work of this, but he omitted you instantiate this step, It's as handy as calling this method directly, and you can overload the method.
Abstract Requirements:
There is at least one abstract method in a class
Abstract method Money must be added abstract
Cases:

The code is as follows Copy Code
Abstract class database{
Abstract public Function connect ();
Abstract public Function query ();
Abstract public function fetch ();
Abstract public Function close ();
}

Note: Abstract methods cannot be defined as private methods and cannot be defined as the final method because they need to be inherited.

4. Passing Object references
PHP4: All "=" are created with a copy
PHP5: In addition to objects, all other "=" assignments are created with a copy, while the object is a reference

5. Cloning objects
One
Aggregation classes:
Introduction to the __call method:
__call is called when the client code uses a method that is not defined in the class.
__call () accepts two parameters, one is the method name, and the other is passed to all arguments (including arrays) to invoke the method
Any value returned by the __call () method is returned to the client, as if the invocation were real
Cases:

The code is as follows Copy Code

Class address{
protected $city;
protected $country;

Public Function setcity ($city) {$this->city = $city;}
Public Function getcity () {return $this->city;}
Public Function Setcountry ($country) {$this->country = $country;}
Public Function Getcountry () {return $this->country;}
}

Class person{
protected $name;
protected $address;
Shallow clone
Public Function __construct () {
$this->address = new address;
}

Public Function SetName ($name) {
$this->name = $name;
}
Public Function GetName () {
return $this->name;
}

Public Function __call ($method, $arguments) {
if (method_exists ($this->address, $method)) {
return Call_user_func_array (Array ($this->address, $method), $arguments);
}
}
Deep clones
Public Function __clone () {
$this->address = Clone $this->address;
}
}

$test 1 = new person;
$test 2 = clone $test 1;

$test 1->setname (' testname1 ');
$test 1->setcity (' testcity1 ');
$test 2->setname (' testname2 ');
$test 2->setcity (' Testcity2 ');

echo $test 1->getname (). ' -'. $test 1->getcity (). " n ";
echo $test 2->getname (). ' -'. $test 2->getcity (). " n ";
Testname1-testcity2
Testname2-testcity2



6. Important Property Access (__set __get __isset __unset) __isset __unset5.1 only useful
Role: Intercept the need for attributes, in order to improve the degree of separation, but also to implement __isset () and __unset (), so that when we use isset to detect attributes or unset () to delete the property, to ensure that the class behaves correctly
Cases:

The code is as follows Copy Code

Class person{
Protected $__data = Array (' email ', ' test ');

Public Function __get ($property) {
if (Isset ($this->__data[$property])) {
return $this->__data[$property];
}else{
return false;
}
}

Public Function __set ($property, $value) {
if (Isset ($this->__data[$property])) {
return $this->__data[$property] = $value;
}else{
return false;
}
}

Public Function __isset ($property) {
if (Isset ($this->__data[$property])) {
return true;
}else{
return false;
}
}

Public Function __unset ($property) {
if (Isset ($this->__data[$property])) {
Return unset ($this->__data[$property]);
}else{
return false;
}
}
}

$test = new Person;
$test->email= ' test ';
Var_dump ($test->email);

Attention:

These two methods only capture missing attributes, and if you define a property for your class, PHP does not call __get () and __set () when accessing this property;
These two methods completely destroy the idea of any attribute inheritance. If there is a __get () method in the parent object, and you implement your own __get () method in the subclass, then your object will not execute correctly because the __get () method of the parent class is never called and can, of course, be resolved with Parent::__get ()

Disadvantages:

Relatively slow speed
Using the magic accessor method, it is not possible to automatically document code using a tool such as the reflection class, such as Phpdocumentor
It cannot be used for static properties

http://www.bkjia.com/PHPjc/628862.html www.bkjia.com true http://www.bkjia.com/PHPjc/628862.html techarticle Object-oriented programming is a commonly used method in PHP, this article introduces the object-oriented simple use of PHP and some basic knowledge of the needs of friends to enter the reference. (OOP) to develop ...

  • 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.