Php5 learning Notes (1)-php Tutorial

Source: Internet
Author: User
Php5 learning records (1) 1. main new features of php5 1. publicprivateprotected access modifier for methods and attributes of objects. You can use common object-oriented access modifiers to control the access level of methods and attributes: classMyClass {private $ id18; publicfunctiongetId () {php5 learning record (1)

I. main new features of php5

1. public/private/protected access modifier for methods and attributes in objects.

You can use common object-oriented access modifiers to control the access level of methods and attributes:

Class MyClass {

    private $id = 18;    public function getId() {        return $this->id;    }}
?

2. Unified constructor name__ construct ().

To prevent the constructor from having the same name as the class name, use _ construct () to declare it, making it easier for you to transfer classes in the class hierarchy:

?

class MyClass {    function __construct() {        print 'Inside constructor';    }}

?

? 3. define the object's destructor through _ destructor.

You can define an Destructor to deregister an object:

?

class MyClass {    function __destruct() {        print ' Destroying object';    }}

?

? 4. interface.

An interface class can load more than one related interface. One class can only inherit from another class, but can implement any number of interfaces:

?

interface Display {    function display();}class Circle implements Display {    function dispaly() {        print ' Dispalying circle \n';    }}

?

? 5. instanceof operator.

Instanceof is used to check whether a given object belongs to (inherited from) a class, a subclass of a class, and an interface ). If yes, true is returned.

?

if ($obj instanceof Circle) {    print '$obj is a Circle';}
?

?

?

6. Final flag method.

The Final keyword allows you to identify a method so that it cannot be overloaded by the quilt class.

?

class MyClass {    final function getBaseClassName() {        return __CLASS__;    }}
?

?

7. Final tag class.

After a class is declared as the final type, it cannot be inherited. The following example reports an error.

?

final class FinalClass {}class BogusClass extends FinalClass {}

?

?

8. force copy object

To clone an object, you must use the clone keyword. You can declare a _ clone () method in the class, which will be called during the cloning process (called after copying attributes and methods from the original object ):

?

class MyClas {    function __clone() {        print 'Object is being cloned';    }}$obj = new MyClass();$obj_copy = clone $obj;
?

?

9. constants in the class.

The class definition contains constants and can be referenced by classes:

?

class MyClass {    const SUCCESS = "Success";    const FAILURE = "Failure";}print MyClass::SUCCESS;

?

? 10. static method.

You can now define static methods and use them without instantiating them. Static methods cannot use the $ this variable because they are not bound to any specific object:

Class MyClass {

    static function helloWorld() {        print 'Hello world';    }}MyClass::helloWorld();

?

11. static members.

The class definition now contains static members (attributes) and can be accessed by the class itself. The most commonly used Singleton mode is:

?

class Singleton {    static private $instance = NULL;    private function __construct() {    }    static public function getInstance() {        if (sell::$instance == NULL) {            self::$instance = new Singleton();        }        return self::$instance;    }}

?

?

12. abstract class.

Declaring a class as an abstract class can prevent it from being instantiated. However, you can inherit an abstract class:

?

abstract class MyBaseClass {    function dispaly() {        print 'Default dispaly routine being called';    }}
?

?

13. abstract methods.

Declare methods as abstract so that they can be defined in the inherited subclass. The class containing the abstract method must be an abstract class:

?

abstact class MyBaseClass {    abstract function dispaly();}

?

? 14. object type prompt.

You can prompt the object type of parameters in the function declaration. If the correct object type is not passed during function call, the system reports the following error:

?

function expectsMyClass(MyClass $obj) {}

?

? 15. support continuous reference of objects returned by methods.

In PHP4, you cannot directly reference objects returned by methods. You must assign a value to an intermediate variable before using it for reference.

Php 4:

?

$dummy = $obj->method();$dummy->method2();

?

? Php 5:

?

$obj->method()->method2();

?

?

16. iteration.

Php5 allows php classes and php inheritance classes to implement the iterator interface. After implementing the iterative interface, you can use the foreach () language structure to traverse an instance of a class:

?

$obj = new MyIteratorImplementaion();foreach ($obj as $value) {    print $value;}

?

?

17. _ autoload () method.

The _ autoload () function is automatically called when you need to use an undefined class. By calling this function, the script engine provides the last chance to load the class before php throws an undefined class error:

?

function __autoload($class_name) {    include_once($class_name . "php");}$obj = new MyClass();$obj2 = new MyClass2();
?

18. exception handling.

Php5 adds an exception handling example of the famous try/throw/catch architecture. During use, you can only throw objects inherited from the Exception class:

Class SQLException extends Exception {

    public $problem;    function __construct($problem) {        $this->problem = $problem;    }}try{ ...     throw new SQLException("Couldn't connect to database"); ...} catch (SQLException $e) {     print "SQLException";} catch (Exception $e) {    print 'Other Exception';}

?

19. The foreach function supports reference.

In php 4, you cannot traverse an array and change its value. However, php 5 allows you to speed up and reference the parameter of the foreach () loop, so that you can change the value of the array in the loop that traverses the array:

?

foreach ($array as &$value) {    if ($value === "NULL") {        $VALUE = NULL    }}

?

? 20. set the default value for the reference parameter.

In php 4, you can only set default values for parameters that pass values. Php 5 now allows you to set the default value for passing referenced parameters:

?

function my_func(&$arg = null) {    if ($arg === NULL) {        print '$arg is empty';    }}my_func();
?

?

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.