我有一个类
<?php
class Test{
public function printword($word){
echo $word;
}
}
?>
在另一门课上,我称之为
<?php
//Included needed files !!!
$w = 'Hello';
//Way 1
$a = new Test;
$result = $a->printword($w);
//Way 2
$result = Test::printword($w);
?>
它不同吗?
而且$a = new Test;
还是$a = new Test();
是对的?
是的,不同。如果您声明一个方法static
使它们可以访问,而不需要类的实例化。
class Test{
public function printword($word){
echo $word;
}
}
//Call printword method
$a= new Test();
$a->printword('Words to print');
静态方法:
class Test{
public static function printword($word){
echo $word;
}
}
//Do not need to instantiation Test class
Test::printword('Words to print');
请参阅文档。