"Learning notes" PHP facing opposite thought two

Source: Internet
Author: User
Tags getmessage parse error

Iv. interface

1. The role of the interface: Define a set of specifications, the method of the interface definition must be fully implemented in the program.

The difference between the interface in 2.PHP and other languages

In PHP, it is only concerned with the method of implementing the interface, but does not care about whether the interface semantics are correct or not, and can implement methods that do not exist in the interface.

However, in Java, the interface is considered to be a type, except that the method that implements the interface is not allowed to invoke methods that do not exist in the interface.

3. Thinking about the PHP interface

PHP interfaces are insufficient in "contract-oriented programming" in relation to java/c++. In PHP, the interface is more like a design document. In general, because of the weak type language in PHP and the emphasis on flexibility, it is not recommended to use the interface on a large scale, only in part of the kernel code, you can more consider the use of abstract classes.

V. Reflection

Find the origin and source according to the destination, such as knowing the owning class and the method owned by an object.

1. Using the Reflection API

classperson{ Public $name;  Public $gender;  Public functionsay () {Echo $this->name. "Is".$this-gender; }}$student=NewPerson ();//Get object Property list$reflict=NewReflectionobject ($student);$props=$reflict-getProperties ();//Print_r ($props);foreach($props  as $prop){    Print $prop->getname (). " \ n ";}//get list of object methods$method=$reflict-GetMethods ();//Print_r ($method);foreach($method  as $prop){    Print $prop->getname (). " \ n ";}

Among them, direct printing props display: Array ([0] = = Reflectionproperty Object ([name] = + Name [class] = person) [1] = Reflection Property Object ([name] = = Gender [class] = person))

Direct Print method Display: Array ([0] = = Reflectionmethod Object ([name] = say [class] = person))

2. The effect of reflection

Can be used for document generation. Scan the class of the file to generate a description document.

Vi. Exceptions and Errors

1. How to use the exception handling mechanism

Exceptions and Errors in PHP: Exceptions-an interruption of logic and business processes, a condition that is not expected in business logic, differs from normal processes, is not a grammatical error, and an error-usually occurs when an abnormal code is encountered.

PHP can catch exceptions only by manually throwing exceptions.

Take the exception of 0 for example

  $a  = 0;  try   { if  ( $a  = = 0 throw  new  exception  (' divisor cannot be 0 ', 1 echo  4/ $a ; //  do not execute  }  catch  (exception   $e  ) { echo   $e ->getmessage ();}  

1) Basic syntax

Try {           // error or exception code           //catch indicates capture, exception is PHP's defined exception class catch(  Exception$e) {           / / for exception handling, method:           //1, Self           -processing//2, do not process, throw it again }

2) Exception Handling class

//A custom exception class that inherits PHP's exception base class exceptionclassMyExceptionextends Exception {    functionGetInfo () {return' Custom error message '; }}

Try { //the function that uses the exception should be in the "try" code block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception is thrown. Throw NewMyException (' error ');//This specifies how the exception is triggered. Note: Each "throw" must correspond to at least one "catch", which can of course correspond to multiple "catch"} Catch(Exception $e) {//the catch block catches the exception and creates an object that contains the exception information Echo $e->getinfo ();//getting custom exception information Echo $e->getmessage ();//gets the GetMessage information inherited from the base class}

Error level of 2.PHP

1) deprecate (not recommended), indicating that the normal process of PHP is not affected, but recommended amendments

2) Notice, the grammar of the wrong place, also does not affect the normal PHP process

3) Warning, high-level error, syntax has very inappropriate circumstances

4) Fetal error (fatal error), directly causes the PHP process to terminate, the following code is no longer executed

5) Parse error (Syntax parsing errors), error for grammar check phase

Vii. Object-Oriented design principles

1. Single responsibility Principle (SRP): Avoid having the same responsibilities spread across different classes and avoid having too many responsibilities for one class.

"Why adhere to the SRP" 1) can reduce the coupling between classes 2) improve the reusability of classes

2. Interface isolation principle (ISP): Clients are not forced to implement some interfaces that are not available, that is, the use of multiple specialized interfaces than the use of a single interface good

• Pollution-contaminated design

• Reduced contamination of the interface

3. Open-Close principle (OCP)

The developer is required to extend the software function of the application system without modifying the existing function code of the system, that is, a module should be open in terms of extensibility and should be closed in terms of change.

4. Replacement principle (LSP)

Subtypes must be able to replace their parent types, and appear anywhere the parent class can appear.

"Problem origin" has a functional P1, which is done by Class A. Now need to extend the function P1, the function of the extension is P, where p is composed of the original function P1 and the new function P2. The new function p is done by subclass B of Class A, and sub-class B, while completing the new function P2, may cause the original function P1 to fail.

"Solution" when using inheritance, follow the Richter substitution principle. When Class B inherits from Class A, try not to rewrite the parent class A's method, but also try not to reload the parent Class A's method, except to add a new method to complete new functionality P2.

5. Dependency inversion principle (DIP)

The upper module cannot depend on the underlying module, and the abstraction cannot depend on the specific.

"Problem origin" Class A directly depends on class B, if you want to change Class A to dependency Class C, you must modify the code of Class A to achieve. In this scenario, Class A is typically a high-level module that is responsible for complex business logic, and Class B and Class C are low-layer modules that are responsible for basic atomic operations, and if Class A is modified, it poses unnecessary risks to the program.

The solution modifies class A to dependent interface I, Class B and Class C each implement interface I, and Class A is indirectly associated with Class B or Class C through interface I, which greatly reduces the chance of modifying Class A.

"Learning notes" PHP facing opposite thought two

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.