简体   繁体   English

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

[英]PHP Call a function from another function in same class

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;
}

}

?>

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小提琴

well it is working if you add the last two lines:好吧,如果您添加最后两行,它就可以工作:

<?php
 class some{

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

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

}

$x = new some;
$x->display();

?>

see here and click on "execute code" 看到这里并点击“执行代码”

Seems you are not called to display() function.似乎您没有被调用 display() 函数。 Call to that function and try again.调用该函数并重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM