PHP Course---Object-oriented

Source: Internet
Author: User
Tags modifiers


Object-oriented:
One: Define Class

class dog{    var$name;     var $age ;     var $pinzhong ;             function Jiao ()    {        echo "{$this->name} is called";    }}

Two: Instantiating an object
$dog = new Dog ();
Call the members of the object:
$dog->name = "Wang Choi";
Call member Method:
$dog->jiao ();

Three, $this keywords (placed in the class represents an object of this class instantiation)
$this, $name; This represents the object, and if you want to invoke member variables in a member method, be sure to use $this to find the member variable.
Full code:

 classDog {var $name; var $age; var $pinzhong; functionJiao () {Echo"{$this->name} is called ";//this represents the current object.    }   }        $dog=NewDog (); $dog->name = "Wang Choi"; $dog->age = 10; $dog->pinzhong = "Husky"; $dog->jiao ()//Calling member Methods


Four, constructor: (constructor is to initialize the class, PHP is not overloaded with functions)
1. Special Execution Time
2. Special wording
The code is as follows:

classDog {var $name;//when writing access modifiers, VAR does not have to be written.    var $age; var $pinzhong; function__construct ($n,$a,$p)//Constructor, note that the two underscore is connected, the front must be a space    {           $this->name =$n; $this->age =$a; $this->pinzhong =$p; }    functionJiao () {Echo"{$this->name} is called ";//this represents the current object.    }   }        $dog=NewDog ("Wang Choi", 10, "Jin Mao"); $dog->jiao ()//Calling member Methods


V. Destructors (cannot have any parameters)

function __destruct ()   {       echo "Goodbye";   }

Six, Package:
Adding a private modifier to a member variable inside a class is intended to protect the members of the class from being arbitrarily accessed by the outside world, thus ensuring the security of the class. If the variable is set to private, how to access:
1. Write the function Get function set function, then assign and value the variable
2. Use the system's own __get () function and the __set () function to implement the value of the variable assignment within the class.

  function __set ($name,$value)        {            $this-$name $value ;        }     function __get ($name)        {            return$this,$ Name;        }


Full code:

 classDog {Private $name;//when writing access modifiers, VAR does not have to be written.    Private $age; Private $pinzhong; function__set ($name,$value)    {        $this-$name=$value; }     function__get ($age)    {        return $this-$age; }    function__construct ($n,$a,$p)      {           $this->name =$n; $this->age =$a; $this->pinzhong =$p; }    functionJiao () {Echo"{$this->name} is called, it {$this->age} years old. "; }    function__destruct () {EchoBye; }   }        $dog=NewDog ("Wang Choi", 10, "Jin Mao"); $dog->name = "Xiao Qiang"; $dog->age = 3; Echo $dog-Age ; $dog->jiao ()//Calling member Methods


VII: Inheritance
Inheritance is generally a single inheritance: a son can have only one father.
Subclasses can inherit all members of the parent class

Class Hashiqi extends Dog//dog is the parent class Hashiqi is the child class
{

}

A subclass can override a member method of a parent class: (like a function name), if you want to invoke a method of the parent class in a subclass method, use Parent::jiao ();

function Jiao ()        {            parent:: Jiao ();  // The parent represents the parents class  :: Represents the calling method, called            with the class name Echo "Hello";        }


Full code:

 classDog {Private $name;//when writing access modifiers, VAR does not have to be written.    Private $age; Private $pinzhong; function__set ($name,$value)    {        $this-$name=$value; }     function__get ($age)    {        return $this-$age; }    function__construct ($n,$a,$p)      {           $this->name =$n; $this->age =$a; $this->pinzhong =$p; }    functionJiao () {Echo"{$this->name} is called, it {$this->age} years old. "; }    function__destruct () {EchoBye; }   }    classHashiqiextendsDog//Dog is the parent class Hashiqi is a subclass    {        functionJiao () {Parent:: Jiao ();//The parent represents the parents class:: Represents the calling method, called with the class name            Echo"Hello"; }    }    $dog=NewHashiqi ("Wang Choi", 10, "Jin Mao"); $dog->name = "Xiao Qiang"; $dog->age = 3; Echo $dog-Age ; $dog->jiao ()//Calling member Methods



The final keyword can define the class as the final class and cannot be inherited

Static:
Keyword: static, if the member declares to be static, then the member belongs to the class, not to the object.
How to invoke a static member:
Hashiqi:: $height = 10;
Hashiqi::gao ();
To invoke a static member of a class with a class name and double colons

PHP Course---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.