Talking about the dependency of the PHP object

Source: Internet
Author: User

Methods of constructing functions

1<?PHP2 //defines a class in which the following classes rely on methods within this class3 classPlay4 {5      Public functionplaying ()6     {7         Echo"I can playing";8     }9 }Ten  One classVideo A { -     Private $action;//Defining private Properties -      Public function__construct ($a)//to make an object argument the     { -         return $this->action =$a; -     } -     //accessing private properties in a class by method +      Public functiongetaction () -     { +         $this->action->playing (); A     } at } -  - $ply=Newplay (); - $vid=NewVideo$ply); - $vid->getaction ();//output I can playing

By property

<?PHP//defines a class in which the following classes rely on methods within this classclassplay{ Public functionplaying () {Echo"I can playing"; }}classvideo{Private $action;//Defining private Properties     Public function__set ($param,$a)//setting property values by the __set () method    {        return $this->action =$a; }    //accessing private properties in a class by method     Public functiongetaction () {return $this->action->playing (); }}$ply=NewPlay ();//instantiate the play class behind the parameters$vid=Newvideo ();$vid->action =$ply;//The __set () method is called when accessing video as a defined property value (you can see the Magic Method of PHP)$vid->getaction ();//output I can playing

Talking about the dependency of the PHP object

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.