在另一个 class 中调用函数的两种方式有什么不同吗?
Is it different between two way to call a function in an other class?
我有一个class
<?php
class Test{
public function printword($word){
echo $word;
}
}
?>
在另一个 class 中,我称之为。
<?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.
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');
我有一个class
<?php
class Test{
public function printword($word){
echo $word;
}
}
?>
在另一个 class 中,我称之为。
<?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.
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');