关于给构造函数传达参数方法

Posted 天涯alone

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于给构造函数传达参数方法相关的知识,希望对你有一定的参考价值。

<?php 
//给构造函数传达参数
header("Content-type: text/html; charset=utf-8");
class test
{
    private $user_ids=array("66645","66586");
    private $user_id;
    //构造传递参数
    public function __construct($user_id)
    {
        $this->user_id=$user_id;
        if(!in_array($this->user_id, $this->user_ids))
        {
            $res[‘status‘]=0;
            $res[‘msg‘]="你的账号不在测试账号内";
            // ajax_return($res);exit;
            print_r($res);exit;
        }
    }
    public function test1($user_id)
    {
        echo "<br/>";
        echo "___1111";    
    }

}

$user_id=66642; 
//new类的时候带参数
$obj=new test($user_id);
$obj->test1($user_id);



 ?>

 

以上是关于关于给构造函数传达参数方法的主要内容,如果未能解决你的问题,请参考以下文章

将初始数据提供给片段的正确方法?

将初始数据提供给片段的正确方法?

当我们将对象作为参数传递给方法时,为啥会调用复制构造函数?

片段真的需要一个空的构造函数吗?

[Catel]如何将带有构造函数参数的 ViewModel 传递给 TabService 扩展方法?

定义一个类Point,代表一个点,public属性有x和y,方法有显示点坐标 show(),构造函数有两个参数分别给x,y赋值,在main方法中构造两个对象,再创建一方法(getMiddle)