JongMin Posted June 28, 2013 Share Posted June 28, 2013 (edited) Hi, all Please look at the code below: *** test.class.php *** class Test { public $test1; public $test2; public $test3; function getTest1() { return $this->test1 = 'Test.'; } function getTest2() { $this->test2 = 'Test.'; } function getTest3() { echo 'Test.'; } } *** test.php *** <?php require_once("test.class.php"); Test = new Test(); // prints Test on the screen echo Test->getTest1(); // prints Test on the screen echo Test->getTest2(); // prints Test on the screen Test->getTest3(); Q1. What is the difference between getTest1() and getTest2()? Q2. Which one is the best way to call the function among these functions for the system performance? Thanks in advance Edited June 28, 2013 by JongMin Quote Link to comment https://forums.phpfreaks.com/topic/279656-question-about-function/ Share on other sites More sharing options...
requinix Posted June 28, 2013 Share Posted June 28, 2013 A1. // prints Test on the screen echo Test->getTest2(); No it doesn't. A2. Irrelevant because all three do slightly different things: first sets a value and returns it, second just sets a value, third just returns a value. Quote Link to comment https://forums.phpfreaks.com/topic/279656-question-about-function/#findComment-1438330 Share on other sites More sharing options...
JongMin Posted June 28, 2013 Author Share Posted June 28, 2013 (edited) Sorry wrong question; ignore the original question and see below: *** test.class.php *** class Test { public $test1; public $test2; function getTest1() { return $this->test1 = 'Test.'; } 7 function getTest2() { $this->test2 = 'Test.'; } function getTest3() { echo 'Test.'; } } *** test.php *** <?php require_once("test.class.php"); Test = new Test(); // prints Test on the screen echo $Test->test1; // prints Test on the screen echo $Test->test2; // prints Test on the screen Test->getTest3(); Q1. What is the difference between getTest1() and getTest2()? Q2. Which one is the best way to call the function among these functions for the system performance? Edited June 28, 2013 by JongMin Quote Link to comment https://forums.phpfreaks.com/topic/279656-question-about-function/#findComment-1438334 Share on other sites More sharing options...
Irate Posted June 28, 2013 Share Posted June 28, 2013 Q1. As requinix said, these functions do all very similar tasks. getTest1() returns a value, getTest2() assigns a value to a variable. If you echo both, you get the same results. Q2. If you are just interested in echoing 'Test', then getTest3() is the fastest as it doesn't inherit any object's methods, doesn't work with variables, it just straight-out echoes 'Test'. Otherwise getTest1() should be the fastest, based on approximated logical conclusions. Quote Link to comment https://forums.phpfreaks.com/topic/279656-question-about-function/#findComment-1438337 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.