简体   繁体   中英

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. Call to that function and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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