dpv46227 2016-03-31 22:08
浏览 272
已采纳

PHP从同一个类中的另一个函数调用一个函数

I am unable figure out how to make this work any help will be appreciated

<?php
 class some{

function display()
{
    $w ="its working";
    $this->show($w);

}
function show($s)
{
    echo $s;
}

}

?>
  • 写回答

3条回答 默认 最新

  • dozabt4329 2016-03-31 22:21
    关注

    You were rightly advised to create an instance of your class then call the method on it but you said

    see thats what i don't want .....i want some way to make it work without adding those two lines...by doing something else...just not that...and i can't figure out what i can do.

    That something else is Simple! Make your method static

    Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.

    public static function display()
    {
        $w ="its working";
        self::show($w);
    }
    

    Then you can just do

    some::display();
    

    Fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部