PHP classes and objects, constructors and destructors, plus this binding

Source: Internet
Author: User

First, class and object

Not only in PHP, but in all object-oriented programming languages, classes are abstract, and objects are an instance of a class. All abstractions are: "Extract the Resemblance".

In life, a person is a class, pull alike is: People have facial features, hair, hands and feet, will eat sleep and so on.

Take a look at the implementation of class and object code in PHP

<?php    class Person{ //定义一个叫Person的类    public  $name; //声明共有的属性:名字    public function say(){ //声明共有的方法:说话        echo ‘saying‘;    }       }?>

Constructors and destructors

A constructor is a method that executes when an object is new, and the destructor is the method that executes when the object is destroyed.

The destruction of the object can be destroyed either explicitly or automatically after the code page finishes executing.

<?php     class person { //Define a class called person     Public  $name;//Declaration of common attributes: First name     Public  function say(){ //Declaration of common methods: speaking        Echo ' saying '; } Public  function __construct(){ //fixed __construct declaration constructor        Echo ' construct ';//Output at object creation time} Public  function __destruct(){        Echo ' Destruct ';//object is destroyed when output}}$a=NewPerson ();//Output construct    //To output destruct after code ends?>

The binding of this

ThIsreturnbackwhenbeforetiedfixedof thetheElephant. in theP H P inasFruitto bein theSquareMethodBodyinTunewiththeElephantinof theGenusSex,Allto bePluson This is otherwise considered to be a local variable.

<?php     class person { //Define a class called person     Public  $name=' Color ';//Declaration of common attributes: First name     Public  function say(){ //Declaration of common methods: speaking        Echo ' saying '; } Public  function __construct(){            Echo $this->name;//Output color, if echo $name will error, because $name is undefined at this time}}$a=NewPerson ();?>

Author: by: Rodan Http://blog.csdn.net/sunyuan_software

PHP classes and objects, constructors and destructors, plus this binding

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.