PHP 方法重写,参数不同,报错: Declaration

前言

php 方法重写,参数不同,报错: Declaration of should be compatible with that

本人laravel报错提示

php 方法重写,参数不同,报错: Declaration of should be compatible with that

这里是
ErrorException: Declaration of Module\Article\Controller\Mobile\ListController::tagsVideo($type, $tagsStr) should be compatible with Module\Article\Controller\Web\ListController::tagsVideo($type, $tagsStr, $page = NULL)

因为我这里是继承的关系,这是手机端的控制器继承PC控制器。

php 方法重写,参数不同,报错: Declaration of should be compatible with that

这边 一个方法 tagsVideo() 是被重写的方法,但是一直报错,查询了一下,原来是 php 继承 重写 函数,则参数不一致问题。

// MOBILE
/**
     * 视频标签内容页 function
     *
     * @param [str] $type
     * @param [str] $tagsStr
     * @param [int] $page
     * @return void
     * @author bobo <email@email.com>
     * @date   2019-12-18
     */
    public function tagsVideo($type, $tagsStr,$page=null)
    {
        $tags = $this->parseTags($tagsStr);
        $count = 10;
        // 加上 $page
        // 读取缓存版
        $articles = $this->paging($this->relationsCache('tagsVideo',$tags, $count*10, Article::CATEGORY_VIDEO), $count, $page);
        // $articles = $this->paging($this->relations($tags, $count*10, Article::CATEGORY_VIDEO), $count, $page);

        // select * from a where id = 5 limit 

        $urlPattern = $this->linkFactory->videoTag($type, $tags);

        return $this->render("articles", $urlPattern, [
            "tagName" => $tagsStr,
            "articles" => $articles,
            "matchType" => $type,
            "tdk" => [
                "topic" => $this->findTdkTags($tagsStr),
            ]
        ]);
    }
//PC
public function tagsVideo($type, $tagsStr,$page=null)

    {

        $tags = $this->parseTags($tagsStr);

        $count = 10;

        // 加上 $page

        $articles = $this->paging($this->relations($tags, $count*10, Article::CATEGORY_VIDEO), $count, $page);

        $urlPattern = $this->linkFactory->videoTag($type, $tags);

        return $this->render("articles", $urlPattern, [

            "tagName" => $tagsStr,

            "articles" => $articles,

            "matchType" => $type,

            "tdk" => [

                "topic" => $this->findTdkTags($tagsStr),

            ]

        ]);

    }

明显看到,是有一个page 参数,我们可以给一个默认为空即可。


详细举一个例子 来讲

报错提示

<?php
abstract class A {
// 方法无参数
public static function foo(){ echo 'bar'; }
}

abstract class B extends A {
// 方法有参数
public static function foo($str){ echo $str; }
}
?>

如上面的代码:类A中的foo方法无参数,类B在继承A后重写foo方法时加入了参数,因此会产生一个类似下面E_STRICT级别的警告:

Strict standards: Declaration of ... should be compatible with that of ...

解决方法:

<?php
abstract class A {
// 方法无参数
public static function foo(){ echo 'bar'; }
}

abstract class B extends A {
// 方法有参数
public static function foo($str = NULL){ echo $str; }
}
?>

解决办法

类B在重写foo方法时为新加入的参数指定一个默认值即可。

本作品采用《CC 协议》,转载必须注明作者和本文链接
嗨,我是波波。曾经创业,有收获也有损失。我积累了丰富教学与编程经验,期待和你互动和进步! 公众号:上海PHP自学中心
wangchunbo
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 10
sane

重写重载 了解一下

5年前 评论
wangchunbo

@sane 我目前是重写了这个function。 你意思是,使用重载吗。

5年前 评论
wangchunbo

@zhp_xixi 我是重写。

5年前 评论
sane

@kuibatian
重写 (Override) 是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变;
重载 (overloading)是在一个类里面,方法名字相同,而参数不同。返回类型可以相同也可以不同;
php 中的 重载

5年前 评论
wangchunbo

@sane 可能是我代码贴的有问题,重新贴了一下。这样子是重写是的。

// mobile
 public function tagsVideo($type, $tagsStr,$page=null)

//PC
public function tagsVideo($type, $tagsStr,$page=null)
5年前 评论

没细看,但是,是 覆盖 了父类的方法么?

5年前 评论
wangchunbo

@qufo 是的覆盖。

5年前 评论

PHP 里面没有重载,只有重写。重载只是间接用另一种方式去实现而已。

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
司机 @ 某医疗行业
文章
309
粉丝
352
喜欢
565
收藏
1131
排名:61
访问:12.6 万
私信
所有博文
社区赞助商