PHP Language-Object oriented

Source: Internet
Author: User
Tags php language

1. Defining classes

class ren{public$name// define member variable var$sex; function Say ()  // definition method {echo "This person is speaking";}}

2. Instantiation

$ren New Ren ();  // instantiation of $ren // Calling member variables $ren->sex= "female"; $ren->say (); // Calling Methods

3.this references

 class   ren{ public   $name   public   $sex   public  function   Say () { echo  ->name."  This man is talking "; // this represents object not class  }}   $ren  =   Ren ();   $ren ->name= "Zhang San" ;   $ren ->say (); 

4. Constructors

 class   ren{ public   $name   public   $sex     function  __construct (," Span style= "color: #800080;" > $sex ) //  constructor  {  $this ->name= $name  ;  $this ->sex =  $sex  ;}}  $ren  = new  Ren ("John Doe", "male"); 

5. Destructors

function __destruct ()  // Destroy object cast memory, close connection, etc.  Auto execute {echo ' The object is about to be destroyed. ";}

6. Encapsulation

classren{Private $name;//to private a member variablePrivate $sex;Private $age;//Encapsulation: Methods for accessing member variables     Public functionSetage ($age)    {        if($age> &&$age<80)        {        $this->age =$age; }    } //Encapsulation: Simple method assignment, automatic invocation    Public function__set ($name,$value)    {    if($name= = "Age")    {        if($value>18 &&$value<80)        {        $this-$name=$value; }    }    Else    {    $this-$name=$value; }    }  //encapsulation: A simple way to get a value     Public function__get ($name)    {    return $this-name; }     }$ren=NewRen ("John Doe", "male")$ren->name = "What"; Echo $ren->sex;

7. Inheritance

class extends ren{// Parent class method overrides function  Say () {parent:: Say ();  //  Echo "This is the method of the subclass";}}

8. Keywords

Final is used to modify the class, and if a class is defined as a final class, the class cannot be inherited.

Final class ren{}

Static statically.

classstudent{Static  Public $class;//Defining static VariablesStatic functionSay ()//Defining static Methods{Echo"AAAA";}} Student::$class= "Hello";//calling static variablesEchoStudent::$classStudent::$class;//calling a static method

Const defines constants in a class

Const // Defining constants

Echo Studen::ss

PHP Language-Object oriented

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.