5 Replies - 571 Views - Last Post: 26 January 2011 - 12:52 PM Rate Topic: -----

#1 flint0131  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 12-January 11

Which is faster and more efficient?

Posted 25 January 2011 - 07:49 AM

I'm currently creating a system and I'm not sure which of these is faster and more efficient?
I have this function called Core that contains a lot of functions.

1.) Use Core's getters to fetch a data in my database
Example : ( i have getUsername in my Core function )
$Core->getUsername(); // to get the username
$Core->getUserid(); // to get the userid


Each getters runs a query, ( e.g. SELECT something FROM somewhere )
I'm currently using this, but I think, for some reasons which I'm not sure of, that this style can somehow slows my scripts. ( Not sure, maybe because of the many queries )

2.) Use Procedural style? I mean, not really ( IDK, not familiar with teh terminologies ). I'll create a function that returns an array of the fetched results ( Gets all the info needed and store it in an array, like : username, userid, gender, etc.. )
Example :
$theArray = $Code->getAllInABunch();
// code to print $theArray



3.) If you have any suggestions, kindly post here. I'm a starter and in need of help. :) I'm like 40% in finishing this system. :> Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Which is faster and more efficient?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,020
  • Joined: 08-June 10

Re: Which is faster and more efficient?

Posted 25 January 2011 - 08:32 AM

View Postflint0131, on 25 January 2011 - 03:49 PM, said:

Each getters runs a query, ( e.g. SELECT something FROM somewhere )
I'm currently using this, but I think, for some reasons which I'm not sure of, that this style can somehow slows my scripts. ( Not sure, maybe because of the many queries )

if you donít have an awfully complex SQL query, I donít see a reason for that.

if you do quite a lot of DB queries (of the same sort), you can use Prepared Statements (save an SQL template on DB and only transmit data). if the returned data are always the same, you may consider saving them in a session.
Was This Post Helpful? 0
  • +
  • -

#3 Xtron  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 28
  • Joined: 20-May 09

Re: Which is faster and more efficient?

Posted 25 January 2011 - 10:01 AM

Setters and getters arent really good for performance anyway. If it's just something like a username you could have a class set variable, or instead of having a function for each SQL query you could just have one function that runs custom queries, and if the same query is being run for the same username, make sure to only run it once and save the username so you don't have to run it again.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5761
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: Which is faster and more efficient?

Posted 25 January 2011 - 10:18 AM

Don't use an class because you can, but because you should. Your $Core just looks like a global wrapper; it's probably not doing anything for you.

User is a good example of where to use a class, e.g.:
$user = getUser(); // $Core->getUser(); ?
$user->getName();
$user->getId();



The getters shouldn't always query the database. The idea of a class is to store state. However, in web pages, the rules are a little different. State for the page begins and ends with the request. If you need more that that, then it's time to go with sessions. Any web app that want to track logged in users probably wants to use sessions.

My getUser might looke like:
function setUser($user) { 
	$_SESSION['user'] = $user;
	return $user;
}

function login($username, $password) { 
	return setUser(getUserFromDb($username, $password));
}


function getUser() {
	if (!isset($_SESSION['user']) ) {
		return setUser(getGuestUser());
	} else {
		return $_SESSION['user'];
	}
}


Was This Post Helpful? 2
  • +
  • -

#5 flint0131  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 12-January 11

Re: Which is faster and more efficient?

Posted 26 January 2011 - 10:53 AM

Thanks for the replies, I'm using PDO to connect to my database so prepared statements are present.
I'm confused on baavgai's code :
$user = getUser(); // $Core->getUser(); ?
$user->getName();
$user->getId();



function getUser has another function under it? like :
function getUser() {
   function getName() {}
   function getId() { }
}



My bad : Core's a class not a function :/
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5761
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: Which is faster and more efficient?

Posted 26 January 2011 - 12:52 PM

More like:
class User {
	const GUEST_NAME = 'guest';
	const GUEST_ID = 0;
	private $id, $username;
	
	public function __construct($id, $username) {
		$this->id= $id;
		$this->username = $username;
	}
	
	public function getName() { return $this->username; }
	
	public function getId() { return $this->id; }
	
	// probably much more stuff in here
}
	
function getUserFromDb($username, $password) {
	$id = getIdFromDb($username, $password);
	if ($id==0) { 
		return new User(User::GUEST_ID,User::GUEST_NAME); 
	} else {
		return new User($id, $username); 
	}
}



You're asking about objects. I assumed it would be clear if I used them. Any of the code offered is confusing, start here: http://www.php.net/m.../oop5.intro.php
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1