Jump to content

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

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

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.

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 by JongMin

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.