I have a page (index) that acts as an entry point to my application.That page will call function sayHi() and display something based on value (normally the function will do a DB query and display information). I want to know the best method to use.
First method : a new object will always be created each time a user does a request (or page refresh).So is it problematic for page performance (or memory) ?
Second method : let's say we have 2 users that query an index page at exactly same time (with different value), what will happen (race condition) ?
Index page
<?php require_once('firstTest.php'); $value = $_GET['params']; // 1st method $first = new FirstTest(); $first->sayHi($value); // 2nd method $second = FirstTest::getInstance(); $second->sayHi($value); firstTest class:
class FirstTest extends Singleton { public function sayHi($value) { echo 'value is : ' .$value; } } firstTest (2nd method)
class FirstTest { public function sayHi($value) { echo 'value is : ' . $value; } } singleton class
abstract class Singleton { protected static $instance; protected final function __construct(){} protected final function __clone(){} public final function __sleep(){ throw new Exception('cannot serialize'); } public static function getInstance(){ if (self::$instance === NULL) self::$instance = new self(); return self::$instance; } }