Jump to content

Question about function


JongMin

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/279656-question-about-function/
Share on other sites

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?

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.