Php object-oriented programming course NOTE _ PHP Tutorial

Source: Internet
Author: User
Php object-oriented programming learning notes. Object-oriented programming is a commonly used method in php. This article introduces the simple use of php object-oriented methods and some basic knowledge for your reference. (OOP) development of object-oriented programming is a common use method in php. This article introduces the simple use of php object-oriented methods and some basic knowledge of friends who need it for reference.

(OOP. Compared with process-oriented development, object-oriented development has many advantages:
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.

Next I will introduce object-oriented programming.

Classes and attributes and methods

Syntax format for defining classes in PHP:

The code is as follows:
Class classname [optional attributes] {
Public $ property [= value];… // Declare a public ID with public, and assign values to a variable.
Function functionname (args) {// member function in the class method
Code }...
// Class method (member function)
}

Generate an object (class instantiation): $ object name = new classname ();
Use object attributes

In a class, you can access a special pointer $ this. when this class is set or accessed through an operation, $ this-> name is used for reference.

The code is as follows:

Class person {
Function _ destruct ()
{Echo "bye! ";}
}
$ A = new person ();


1. final
Final: php5 adds a final keyword. If the method in the parent class is declared as final, the subclass cannot overwrite the method. if a class is declared final, it cannot be inherited.

The code is as follows:

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 (PHP5.2 or later is recommended)

The code is as follows:
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

Interface: you want to ensure that a class implements one or more methods according to the specific name, visibility, and prototype.
Interface Requirements:
Classes are all abstract methods.
Abstract method: Do not add abstract
The abstract method attribute of the interface is public.
The member attribute must be a constant.
Example:

The code is as follows:
Interface ChildTest {
Public function childTest ();
}
Class FathTest implements ChildTest1, ChildTest2 {
Public function childTest (){
Echo 1;
}
............
}


Abstract: In fact, the abstract class is very similar to the interface class. when I see such a sentence, the abstract class extracts the class image part. this sentence looks funny, in fact, it tells the truth of an abstract class. the function of an abstract class is that when you find that many methods are used in many of your classes and you are repeatedly writing them, you can consider using an abstract class, you may say, "I can't rewrite a class, each public class, I instantiate a common class, and you can call the same method, in fact, this is the work of the abstract class, but it saves you the step of instantiation, making it as convenient as directly calling this class method, and you can also reload this method.
Abstract requirements:
Class has at least one abstract method
Abstract method money must be added abstract
Example:

The code is as follows:
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 or final methods because they need to be inherited.

4. pass object reference
Php4: all "=" is to create a copy.
Php5: except for objects, when other "=" values are assigned, a copy is created, and the object is referenced.

5. Clone object
I,
Aggregation class:
_ Call method introduction:
When the client code uses an undefined method in the class, __call is called.
_ Call () accepts two parameters. one is the method name, and the other is all parameters passed to the method to be called (including arrays)
Any value returned by the _ call () method will be returned to the customer, as if the call method actually exists.
Example:

The code is as follows:

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;
// Shortest 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 clone
Public function _ clone (){
$ This-> address = clone $ this-> address;
}
}

$ Test1 = new Person;
$ Test2 = clone $ test1;

$ Test1-> setName ('testname1 ');
$ Test1-> setCity ('testcity1 ');
$ Test2-> setName ('testname2 ');
$ Test2-> setCity ('testcit2 ');

Echo $ test1-> getName (). '-'. $ test1-> getCity (). "n ";
Echo $ test2-> getName (). '-'. $ test2-> getCity (). "n ";
// Testname1-testcity2
// Testname2-testcity2



6. important attribute access (_ set _ get _ isset _ unset) _ isset _ unset5.1 is useful.
Purpose: Intercept attribute requirements. to improve the degree of separation, we also need to implement _ isset () and _ unset () so that when we use isset to detect attributes or unset () to delete the attribute to ensure that the behavior of the class is correct.
Example:

The code is as follows:

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 );

Note:

These two methods only capture missing attributes. if you define an attribute for your class, php will not call _ get () when accessing this attribute () and _ set ();
These two methods completely undermine the idea of any property inheritance. If the parent object has a _ get () method and you implement your _ get () method in the subclass, your object will not be correctly executed, because the _ get () method of the parent class will never be called, you can use parent ::__ get () to solve the problem.

Disadvantages:

Relatively slow
Using magic accessors is impossible to use reflection classes. tools such as phpjavasentor can automatically document code.
You cannot use static attributes.

Bytes. (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.