Php object-oriented (3) and php object-oriented

Source: Internet
Author: User

Php object-oriented (3) and php object-oriented

1. methods defined by the final keyword cannot be overwritten.

Because the show method is modified by final, an error is returned when the show method is rewritten in the subclass.

<?php    class MyClass {        final function show(){            echo "hello world" . PHP_EOL;        }    }    class MyTest extends MyClass {        function show(){            echo __CLASS__ . PHP_EOL;        }    }    ?>

2. The class defined by final cannot be inherited.

<?php    final class MyClass {        function show(){            echo "hello world" . PHP_EOL;        }    }    class MyTest extends MyClass {    }    ?>

3 ,__ toString Method

If the _ toString method is defined, when an object is printed, _ toString is called.

class Person {    private $name;    function __construct( $name ){        $this->name = $name;    }    function __toString(){        return $this->name;    }}$p = new Person( "ghostwu" );var_dump( $p );echo PHP_EOL;print $p . PHP_EOL;

4. Exception Handling (try, catch, throw)

> Exception handling classes should all inherit from the system Exception

> When an exception is thrown (a throw exception object), the System Searches for exception handling after the catch object. If the catch object matches the catch object, the catch object will not be executed even if it matches the catch object.

Exception summary:

Exception {/* attribute */protected string $ message; protected int $ code; protected string $ file; protected int $ line; /* Method */public _ construct ([string $ message = "" [, int $ code = 0 [, Throwable $ previous = NULL]) final public string getMessage (void) final public Throwable getPrevious (void) final public int getCode (void) final public string getFile (void) final public int getLine (void) final public array getTrace (void) final public string getTraceAsString (void) public string _ toString (void) final private void _ clone (void )}

 

1 class NullHandleException extends Exception {2 function _ construct ($ message) {3 parent ::__ construct ($ message); 4} 5} 6 7 function printObj ($ obj) {8 if ($ obj = NULL) {9 throw new NullHandleException ("printObj receives a null object"); 10} 11 print $ obj. PHP_EOL; 12} 13 14 class Person {15 private $ name; 16 function _ construct ($ name) {17 $ this-> name = $ name; 18} 19 function _ toString () {20 return $ this-> name; 21} 22} 23 24 try {25 printObj (new Person ("ghostwu ")); 26 printObj (null); 27 printObj (new Person ("zhangsan"); 28} catch (NullHandleException $ exception) {29 print $ exception-> getMessage (). PHP_EOL; 30 print "in file :". $ exception-> getFile (). PHP_EOL; 31 print "in line :". $ exception-> getLine (). PHP_EOL; 32} catch (Exception $ exception) {33 echo "this Exception branch will not be executed ". PHP_EOL; 34 print $ exception-> getMessage (). PHP_EOL; 35} 36 37 echo "try... catch processing completed ". PHP_EOL;

Output result:

Ghostwu @ ubuntu :~ /Php_study/php5/03 $ php-f exception_usage.php ghostwuprintObj receives a null object in file:/home/ghostwu/php_study/php5/03/exception_usage.phpin line: 10try... catch process complete

No "zhangsan" output, because printObj (null); throws an exception, so "zhangsan" is ignored

5 ,__ autoload automatically Loads files

In project development, it is often necessary to include multiple common library files in a file, which will generate a lot of require or include, the _ autoload function can simplify the function loading process.

1. MyClass. php

1 class MyClass {2     function printHelloWorld(){3         echo "Hello World" . PHP_EOL;4     }5 }

2, common. inc

function __autoload( $className ) {        require_once( "./{$className}.php" );    }

3, main. php

require_once "common.inc";        $obj = new MyClass();        $obj->printHelloWorld();

 

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.