PHP run function in class [closed]
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;
精彩评论