开发者

PHP run function in class [closed]

开发者_开发技巧 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

I have some code like this inside a file script.php:

class ABC
{
    public function sampleName() { do something}
}

Now if I run

php script.php 

from command line it doesn't do anything. I thought it would execute the function. Do I have to call the funciton sampleName somewhere inside the file? If so, how do I do this?

After the class ends should I say $this->SampleName()?


instantiate your class -> call function:

class ABC
{
    function __construct { some code }
    public function sampleName() { do something}
}

$abc = new ABC(); // new instance of ABC
$abc->sampleName(); // we've done something


Reading the manual also helps to understand the basics of OOP in PHP.


After the class closing brace, create the object of the class.

$abc = new ABC();
$output = $abc->sampleName();
print $output;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜