Introduction to php object dependencies

不言
Release: 2023-04-02 11:20:02
Original
2253 people have browsed it

This article mainly introduces the introduction of PHP object dependencies, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Methods through constructors

 1 <?php 
 2 //定义一个类,后面的类依赖这个类里面的方法 
 3 class play 
 4 { 
 5     public function playing() 
 6     { 
 7         echo "I can playing"; 
 8     } 
 9 }
 10 
 11 class video
 12 {
 13     private $action;//定义私有属性
 14     public function __construct($a)//将对象做参数
 15     {
 16         return $this->action = $a;
 17     }
 18     //通过方法访问类中的私有属性
 19     public function getaction()
 20     {
 21         $this->action->playing();22     }
 23 }
 24 
 25 $ply = new play();
 26 $vid = new video($ply);
 27 $vid->getaction();//输出I can playing
Copy after login

Through attributes

<?php
//定义一个类,后面的类依赖这个类里面的方法
class play
{    
public function playing()
    {        
    echo "I can playing";
    }
}class video
{    private $action;//定义私有属性
    public function __set($param, $a)
    //通过__set()方法设置属性值    
    {        
    return $this->action = $a;
    }    //通过方法访问类中的私有属性
    public function getaction()
    {       return $this->action->playing();
    }
}$ply = new play();//实例化play类后面做参数
$vid = new video();
$vid->action = $ply;//当访问video为定义的属性值时会调用__set()方法(不懂的可以看看php的魔术方法)
$vid->getaction();//输出I can playing
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

php code to traverse all files and sub-files in a folder

php remove project file BOM headers in batches Methods

The above is the detailed content of Introduction to php object dependencies. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!