php 在类方法中调用函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1725165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a function within a Class method?
提问by WAC0020
I have been trying to figure out how to go about doing this but I am not quite sure how.
我一直在试图弄清楚如何去做,但我不太确定如何去做。
Here is an example of what I am trying to do:
这是我正在尝试做的一个例子:
class test {
public newTest(){
function bigTest(){
//Big Test Here
}
function smallTest(){
//Small Test Here
}
}
public scoreTest(){
//Scoring code here;
}
}
Here is the part I am having problems with, how do I call bigTest()?
这是我遇到问题的部分,如何调用 bigTest()?
回答by Sergey Kuznetsov
Try this one:
试试这个:
class test {
public function newTest(){
$this->bigTest();
$this->smallTest();
}
private function bigTest(){
//Big Test Here
}
private function smallTest(){
//Small Test Here
}
public function scoreTest(){
//Scoring code here;
}
}
$testObject = new test();
$testObject->newTest();
$testObject->scoreTest();
回答by pjbeardsley
The sample you provided is not valid PHP and has a few issues:
您提供的示例不是有效的 PHP,并且存在一些问题:
public scoreTest() {
...
}
is not a proper function declaration -- you need to declare functions with the 'function' keyword.
不是一个正确的函数声明——您需要使用 'function' 关键字来声明函数。
The syntax should rather be:
语法应该是:
public function scoreTest() {
...
}
Second, wrapping the bigTest() and smallTest() functions in public function() {} does not make them private — you should use the private keyword on both of these individually:
其次,将 bigTest() 和 smallTest() 函数包装在 public function() {} 中不会使它们成为私有的——您应该分别对这两个函数使用 private 关键字:
class test () {
public function newTest(){
$this->bigTest();
$this->smallTest();
}
private function bigTest(){
//Big Test Here
}
private function smallTest(){
//Small Test Here
}
public function scoreTest(){
//Scoring code here;
}
}
Also, it is convention to capitalize class names in class declarations ('Test').
此外,在类声明('Test')中将类名大写是惯例。
Hope that helps.
希望有帮助。
回答by Meganathan S
class test {
public newTest(){
$this->bigTest();
$this->smallTest();
}
private function bigTest(){
//Big Test Here
}
private function smallTest(){
//Small Test Here
}
public scoreTest(){
//Scoring code here;
}
}
回答by Ali Hasan
I think you are searching for something like this one.
我想你正在寻找这样的东西。
class test {
private $str = NULL;
public function newTest(){
$this->str .= 'function "newTest" called, ';
return $this;
}
public function bigTest(){
return $this->str . ' function "bigTest" called,';
}
public function smallTest(){
return $this->str . ' function "smallTest" called,';
}
public function scoreTest(){
return $this->str . ' function "scoreTest" called,';
}
}
$test = new test;
echo $test->newTest()->bigTest();
回答by Gumbo
You need to call newTestto make the functions declared inside that method “visible” (see Functions within functions). But that are then just normal functions and no methods.
您需要调用newTest以使该方法内声明的函数“可见”(请参阅函数内的函数)。但这只是正常的功能,没有方法。
回答by blockhead
In order to have a "function within a function", if I understand what you're asking, you need PHP 5.3, where you can take advantage of the new Closure feature.
为了拥有“函数中的函数”,如果我理解您的要求,您需要 PHP 5.3,您可以在其中利用新的 Closure 功能。
So you could have:
所以你可以有:
public function newTest() {
$bigTest = function() {
//Big Test Here
}
}
回答by Ingeniero
To call any method of an object instantiated from a class (with statement new), you need to "point" to it. From the outside you just use the resource created by the new statement.
Inside any object PHP created by new, saves the same resource into the $this variable.
So, inside a class you MUST point to the method by $this.
In your class, to call smallTestfrom inside the class, you must tell PHP which of all the objects created by the new statement you want to execute, just write:
要调用从类实例化的对象的任何方法(使用语句 new),您需要“指向”它。从外部,您只需使用由 new 语句创建的资源。在由 new 创建的任何对象 PHP 中,将相同的资源保存到 $this 变量中。因此,在类中,您必须通过 $this 指向该方法。在您的类中,smallTest要从类内部调用,您必须告诉 PHP 您要执行 new 语句创建的所有对象中的哪一个,只需编写:
$this->smallTest();
回答by Masoud Siahkali
class sampleClass
{
public function f1()
{
return "f1 run";
}
public function f2()
{
echo ("f2 run" );
$result = $this->f1();
echo ($result);
}
f2();
}
output :
输出 :
f2 run f1 run
f2跑 f1跑
回答by AlexioVay
You can also use self::CONSTinstead of $this->CONSTif you want to call a static variable or function of the current class.
如果要调用当前类的静态变量或函数,也可以使用self::CONST代替$this->CONST。
回答by zloctb
example 1
示例 1
class TestClass{
public function __call($name,$arg){
call_user_func($name,$arg);
}
}
class test {
public function newTest(){
function bigTest(){
echo 'Big Test Here';
}
function smallTest(){
echo 'Small Test Here';
}
$obj=new TestClass;
return $obj;
}
}
$rentry=new test;
$rentry->newTest()->bigTest();
example2
例子2
class test {
public function newTest($method_name){
function bigTest(){
echo 'Big Test Here';
}
function smallTest(){
echo 'Small Test Here';
}
if(function_exists( $method_name)){
call_user_func($method_name);
}
else{
echo 'method not exists';
}
}
}
$obj=new test;
$obj->newTest('bigTest')

