Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我如何引用“a”;这";函数参数中的类成员?_Php_Function_Parameters_This - Fatal编程技术网

Php 我如何引用“a”;这";函数参数中的类成员?

Php 我如何引用“a”;这";函数参数中的类成员?,php,function,parameters,this,Php,Function,Parameters,This,我目前有一个函数,试图引用类中的$id,但它不起作用: public function getCourseInfo($cid = $this->id, $all = false) { } 这是我的班级: class Course { protected $course; protected $id; public function __construct($cid) { $id = $cid; $this->cou

我目前有一个函数,试图引用类中的$id,但它不起作用:

public function getCourseInfo($cid = $this->id, $all = false)
{

}
这是我的班级:

class Course
{
    protected $course;
    protected $id;

    public function __construct($cid)
    {
        $id = $cid;
        $this->course = $this->getCourseInfo($this->id);
    }
    public function getCourseInfo($cid = $this->id, $all = false)
    {

    }
}

您尚未建立$this->id.:)


您的类还缺少一个右大括号。

您尚未建立$this->id.:)


您的类中还缺少一个右大括号。

您需要先在构造函数中设置$id

class Course
{
    protected $course;
    protected $id;
}

public function __construct($cid)
{
    $this->id = $cid;
    $this->course = $this->getCourseInfo($id);
}

您需要首先在构造函数中设置$id

class Course
{
    protected $course;
    protected $id;
}

public function __construct($cid)
{
    $this->id = $cid;
    $this->course = $this->getCourseInfo($id);
}
试试这个

public function getCourseInfo($cid = 'default', $all = false)
{
    $cid = $cid == 'default' ? $this->id : $cid;

}
或者你需要彻底改变你的课程

class Course
{
    protected $course;
    protected $id;

    public function __construct($cid)
    {
        $this->id = $cid;
        $this->course = $this->getCourseInfo();
    }

    public function getCourseInfo($course_id = 0, $all = false)
    {
       $course_id = !$course_id ? $this->id : $course_id;
       //do Somthing with
       //return var;
    }
试试这个

public function getCourseInfo($cid = 'default', $all = false)
{
    $cid = $cid == 'default' ? $this->id : $cid;

}
或者你需要彻底改变你的课程

class Course
{
    protected $course;
    protected $id;

    public function __construct($cid)
    {
        $this->id = $cid;
        $this->course = $this->getCourseInfo();
    }

    public function getCourseInfo($course_id = 0, $all = false)
    {
       $course_id = !$course_id ? $this->id : $course_id;
       //do Somthing with
       //return var;
    }

不,这是不可能的,正如在:

默认值必须是常量 表达式,而不是(例如)a 变量、类成员或函数 打电话

相反,您可以简单地传入null作为默认值,并在函数中更新它

class Course
{
    protected $course;
    protected $id;

public function __construct($cid)
{
    $this->id = $cid;
    $this->course = $this->getCourseInfo($this->id);
}
    function getCourseInfo($cid = null, $all = false) {
        $cid = isset($cid) ? $cid : $this->id;
        ....
    }
}

不,这是不可能的,正如在:

默认值必须是常量 表达式,而不是(例如)a 变量、类成员或函数 打电话

相反,您可以简单地传入null作为默认值,并在函数中更新它

class Course
{
    protected $course;
    protected $id;

public function __construct($cid)
{
    $this->id = $cid;
    $this->course = $this->getCourseInfo($this->id);
}
    function getCourseInfo($cid = null, $all = false) {
        $cid = isset($cid) ? $cid : $this->id;
        ....
    }
}

此线程中的每个人都给出了正确答案,但没有人给出完整的代码示例,因此我将发布我的建议:

您会注意到,我在getCourseInfo()中省略了CourseID参数,因为如果您用课程id实例化类,则不需要它

其次,我认为不应该在构造函数中调用getCourseInfo,因为这些信息只会在以后需要。此外,我还在函数中添加了“缓存”,这样您就不会两次获取数据


显然,如果我没有看到你的代码,我很可能会出错,但我觉得这是一个更好的代码结构。

此线程中的每个人都给出了正确的答案,但没有人给出完整的代码示例,因此我将发布我的建议:

您会注意到,我在getCourseInfo()中省略了CourseID参数,因为如果您用课程id实例化类,则不需要它

其次,我认为不应该在构造函数中调用getCourseInfo,因为这些信息只会在以后需要。此外,我还在函数中添加了“缓存”,这样您就不会两次获取数据


显然,如果我没有看到您的代码,我很有可能是错的,但我觉得这是一个更好的代码结构。

我假设他正在尝试为具有对象id的第一个参数设置默认值?但是,您是否可以创建一个不带id的重载函数?然后默认为对象idIf only PHP支持的真正重载:-((是的,这个特定示例可以用可选参数近似表示,但通常我的抱怨是正确的!)我假设他正试图为第一个参数设置一个默认值,该参数的id为对象的id?shrugsh您是否会创建一个不带id的重载函数?然后默认为对象idIf only PHP支持的真正重载:-((是的,这个特殊的例子可以用可选的参数来近似,但总的来说我的抱怨是成立的!)是时候开始打破旧的var_dump()了!还有,你有没有抛出任何错误消息?你能展示一下你是如何创建这个对象的吗?需要更多信息!:)这是实际函数“$this”上的语法错误。在我调用它时不是这样。看起来类上也缺少一个右大括号。能否在注释中粘贴准确的错误消息?请删除默认的$cid参数($cid=$this->id,)然后看看它是否仍然损坏。确保你也听从了我在原始答案中的建议。是时候开始破坏旧的var_dump()!还有,你是否抛出任何错误消息?你能展示你是如何创建对象的吗?需要更多信息!:)这是实际函数“$this”上的语法错误。在我调用它时不是这样。看起来类上也缺少了一个右大括号。你能在注释中粘贴准确的错误消息吗?去掉默认的$cid参数($cid=$this->id),看看它是否仍然中断。确保你也在原始答案中听取了我的建议。