Example introduction to some advanced object-oriented programming features of PHP_php skills

WBOY
Release: 2016-05-16 20:04:03
Original
819 people have browsed it

Generally speaking, learning PHP requires understanding the following features:

Object cloning. One of the major improvements to the OOP model in PHP5 is to treat all objects as references instead of values. But if all objects are treated as references, how do you create a copy of an object? The answer is by cloning the object.

<&#63;php
class Corporate_Drone{
 private $employeeid;
 private $tiecolor;
 function setEmployeeID($employeeid) {
 $this->employeeid = $employeeid;
 }

 function getEmployeeID() {
 return $this->employeeid;
 }
 
 function setTiecolor($tiecolor) {
 $this->tiecolor = $tiecolor;
 }
 
 function getTiecolor() {
 return $this->tiecolor;
 }
}

$drone1 = new Corporate_Drone();
$drone1->setEmployeeID("12345");
$drone1->setTiecolor("red");
$drone2 = clone $drone1;
$drone2->setEmployeeID("67890");

printf("drone1 employeeID:%d <br />",$drone1->getEmployeeID());
printf("drone1 tie color:%s <br />",$drone1->getTiecolor());
printf("drone2 employeeID:%d <br />",$drone2->getEmployeeID());
printf("drone2 tie color:%s <br />",$drone2->getTiecolor());
&#63;>

Copy after login

Inherited. As mentioned earlier, building a class hierarchy through inheritance is a key concept of OOP.

class Employee {
 ...
}

class Executive extends Employee{
 ...
}

class CEO extends Executive{
 ...
}

Copy after login

Interface. An interface is a collection of unimplemented method definitions and constants, which is equivalent to a class blueprint. The interface only defines what the class can do, but does not involve implementation details. This chapter introduces PHP5's support for interfaces and provides some examples that demonstrate this powerful OOP feature.

<&#63;php
interface IPillage
{
 // CONST 1;
 function emptyBankAccount();
 function burnDocuments();
}

class Employee {

}

class Excutive extends Employee implements IPillage {
 private $totalStockOptions;
 function emptyBankAccount() {
 echo "Call CFO and ask to transfer funds to Swiss bank account";
 }
 function burnDocuments() {
 echo "Torch the office suite.";
 }
}

class test {
 function testIP(IPillage $ib) {
 echo $ib->emptyBankAccount();
 }
}
$excutive = new Excutive();
$test = new test();
echo $test->testIP($excutive);
&#63;>

Copy after login

Abstract class. Abstract classes are essentially classes that cannot be instantiated. An abstract class will be inherited by an instantiable class, which is called a concrete class. An abstract class can be fully implemented, partially implemented, or not implemented at all.

abstract class Class_name
{
 //insert attribute definitions here
 //insert method definitions here
}
Copy after login

Namespace. Namespaces can divide various libraries and classes according to context, helping you manage your code base more effectively.

<&#63;php
namespace Library;
class Clean {
 function printClean() {
 echo "Clean...";
 }
}
&#63;>

<&#63;php
include "test.php";
$clean = new \Library\Clean();
$clean->printClean();
&#63;>

Copy after login

If you have used other object-oriented languages, you may be wondering why the above features do not include some OOP features that are familiar in other languages? The reason is simple, PHP does not support these features. To save you from confusion, here is a list of advanced OOP features that PHP does not support.

  • Method overloading. PHP does not support polymorphism through function overloading, and according to discussions on the Zend website, it may never be supported. To know the specific reasons, you can check http://www.zend.com/php/ask_experts.php
  • Operator overloading. There is currently no support for giving operators new meanings based on the type of data being modified. According to the discussion on the zend website, it is unlikely that this feature will be implemented in the future.
  • Multiple inheritance. PHP does not support multiple inheritance. But it supports implementing multiple interfaces.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template