class test {
public newTest(){
function bigTest(){
//Big Test Here
}
function smallTest(){
//Small Test Here
}
}
public scoreTest(){
//Scoring code here;
}
}
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();
其次,在 public function (){}中包装 bigTest ()和 small Test ()函数并不会使它们成为私有函数ーー您应该分别对这两个函数使用 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;
}
}
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();
例子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')
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.
在 new 创建的任何对象 PHP 中,将相同的资源保存到 $this 变量中。
因此,在类中,您必须通过 $this 指向该方法。
In your class, to call smallTest from inside the class, you must tell PHP which of all the objects created by the new statement you want to execute, just write:
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();
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;
}
}