PHP Object-oriented three major features

Source: Internet
Author: User

1. Package:

Purpose: Make classes more secure

Steps:
1. The member variable becomes private (private)
2. Set method/Call method
3. Increase the limit in the method

<?PHPclassshao{Private $aa;//it has to be private.    Private $BB;  Public functionSET_AA ($v)    {        $this-&GT;AA =$v; }     Public functionGet_aa () {return $this->aa;//give a return value to AA   }    Public functionSET_BB ($v)   {       if($v>0&&$v<188)//Add a limit to BB       {           $this->bb=$v; }   }}$shili=NewShao ();$shili->set_aa (' Lee ');//Var_dump ($shili);//echo $shili->get_aa ();$shili-&GT;SET_BB (20);Var_dump($shili);?>

Magic Method:

<?PHPclassshao{Private $aa; Private $BB; function__set ($name,$value)    {        //todo:implement __set () method.        if($name= = ' BB ')        {            if($value>0&&$value<188)            {                $this-$name=$value; }        }    }    function__get ($name)    {        //todo:implement __get () method.        return $this-$name; }}$shili=NewShao ();$shili->__set (' BB ', 8);//The BB is placed in the name of set and the parameter is variable.Echo $shili->__get (' BB ');//Take BB?>

2. Inheritance:

Properties and methods for subclasses and parent classes
The subclass inherits all the contents of the parent class, but the private part of the parent class cannot be accessed directly
The newly added properties and methods in the subclass are extensions to the parent class
A property defined in a subclass with the same name as the parent class is an override of the parent class property, and a method of the same name overrides the parent class method.

<?PHPclassshao{ Public functionSasa$name){        Echo $name, ' Dog! <br/> '; }}classZhenextendsshao{ Public functionaaa () {Echo' Cat <br/> '; }}$lli=NewZhen ();$lli-aaa ();$lli-&GT;AAA (' Binghh ');?>

Override method, detailed in PHP object-oriented overrides and overloads * *

3. Polymorphism:

When a parent refers to an instance of a subclass, because the child class overrides the method of the parent class, the parent class reference behaves differently when calling the method, called a polymorphic

Polymorphism refers to a property or behavior defined in a parent class after the quilt class inherits.

can have different data types or behave differently. This makes the same attribute or behavior have different semantics in the parent class and its subclasses.
This means that the same method differs from the result of executing in a subclass with a parent class.

Conditions:
1. To have inheritance
2. Parent class reference to child class instance
3. To have a rewrite
4. Method of Rewriting

Compiling polymorphic ********************

You can make a method inside a class produce multiple effects, depending on the parameters passed in, you can perform different logic

<?PHPclassfu{ Public functiontext () {Echo' Parent class '; }     Public  functionTextt () {EchoFather; }}classErziextendsfu{functionTest$name)        {            //Method OverloadingParent::text ();//I want something from the father.            Echo"Erzi",$name;//rewrite        }    }$erzi=Newfu ();$erzi-Text ("123");?>

Above is the overloaded method, see the PHP object-oriented overrides and overloads in detail * *

PHP Object-oriented three major features

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.