includes vs classes

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

swedstar
Forum Newbie
Posts: 3
Joined: Mon Jan 23, 2006 12:22 pm

includes vs classes

Post by swedstar »

Hi all

I'm fairly new to PHP programming and have now been assigned to work on a PHP project (i'm normally coding in Java). My first reaction on the code that has been written so far is that every time a database connection is to be established an include file with the db connection is included. Well, personally I would have taken a more OO oriented approach and created a DB class and then instantiate that particular obj everytime I need a connection. My question is whether it's more efficient and effective to use includes to establish DB connections than actually encapsulate this in an object? Are objects a more expensive operation in PHP compared to instantiation of classes.

Many thanks

Erik
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I personally perfer to have a DB wrapper class. The connection will be established once only for each needed database and then the resource will be stored in the session. There is a small overhead in instantiating classes to create objects in PHP but for the extra ease of maintenance, sensible logic and clean design I certainly prefer it. Using the __autoload() function as of PHP5 you can include classes as and when you make calls to them too :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I am sure you will get a variety of opinions about this. Here's mine:

Coming from Java to PHP you will first have to make peace with some of the differences -- the biggest being no application server so every request recreates all objects. Just don't let yourself be bothered by it. It's called a "shared-nothing" architecture and there are long arguments about it vs. Java. Ultimately they are just different -- that's all.

I also think you need to see the differece between include and classes. In Java they are the same thing, but while it is a convention to have one class per file in PHP, it really means something different than in Java.

Definitely follow you instincts and create database classes. Typically in PHP there is a Connection class that other DB classes such as TableDataGateway or DataMapper classes will use. Many of the same patterns used in Java are also used in PHP, but the implementations are different so look around for examples.

Also, because in PHP you "reconnect" each request, PHP pools database connections. Take a look at the "pconnect" functions for your database to see about this.

As far as what is more expensive, I would really not focus on that and just build it the way you think is right. PHP is very efficient and Premature Optimization will probably get you in more trouble in the long run than following you instincts.

Many PHP OO programmers use a set of classes that you might be very familiar with from Java. A typical PHP MVC setup would have Request and Response classes, InterceptingFilter and FrontController on the front end, dispatched Actions using the Command pattern, etc. ServiceLocator or some very lightweight DependencyInjection are pretty common solutions for making things like the Request and that DB Connection object discussed available where needed. Let me know if you can't find examples.
(#10850)
swedstar
Forum Newbie
Posts: 3
Joined: Mon Jan 23, 2006 12:22 pm

Post by swedstar »

Hi and thanks for your replies!


d11wtq:
When you say you leave the resource stored in the session is your connection currently open in that case so you only have to generate resultsets on that connection? My approach would be to open and close (using functions) the connection everytime I need it which result in that I have to instantiate the DB object on every page when I need data from the database.


Arborint said that PHP reconnect each request how does this affect if you put your connection in session scope?
Another thing I also should ask is if resultsets are taking up resources in terms of active database connections. In java you ideally wants to get the data into a collection type so as soon as possible so you don't need to pass expensive resultsets around within the system.

It would be interested to find articles that describe large-scale development using PHP. Thanks Arborint for mentioning design patterns now I get new interesting search results when I search google :) ( I just didn't think of searching on design patterns and php)

Thanks
Erik
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

swedstar wrote:Arborint said that PHP reconnect each request how does this affect if you put your connection in session scope?
I would not keep connection objects in the session. As I said, PHP keeps a connection pool so it is fine to just do a pconnect each time.
swedstar wrote:Another thing I also should ask is if resultsets are taking up resources in terms of active database connections. In java you ideally wants to get the data into a collection type so as soon as possible so you don't need to pass expensive resultsets around within the system.
No, typically you would only have one connection and pass that object (containing the link resource) to a RecordSet (the simplest solution) or ActiveRecord. Again, you don't need to be as concerned with things being "expensive" because everything is freed when the script exits.
swedstar wrote:It would be interested to find articles that describe large-scale development using PHP. Thanks Arborint for mentioning design patterns now I get new interesting search results when I search google :) ( I just didn't think of searching on design patterns and php)
Take a look at some of the frameworks around Symphony, Prado, Wact, etc. (there are many more). Don't look at the PHP Code section of this forum and think that's how Enterprise PHP code looks. :oops:
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

arborint wrote:Don't look at the PHP Code section of this forum and think that's how Enterprise PHP code looks. :oops:
Hahaha :P
swedstar
Forum Newbie
Posts: 3
Joined: Mon Jan 23, 2006 12:22 pm

Post by swedstar »

Hi,

thanks for your feedback guys. Now I have got a good starting point and it's time to do abit of research...

Cheers
Erik
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

arborint wrote:
swedstar wrote:Arborint said that PHP reconnect each request how does this affect if you put your connection in session scope?
I would not keep connection objects in the session. As I said, PHP keeps a connection pool so it is fine to just do a pconnect each time.
Using pconnect on a medium to high volume website isn't really a good idea..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Jcart wrote:Using pconnect on a medium to high volume website isn't really a good idea..
I haven't noticed that problem and have some sites that have hundreds of concurrent users. I'm interested in ways you have found for better performanance. Can you give more detail?
(#10850)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

arborint wrote:
Jcart wrote:Using pconnect on a medium to high volume website isn't really a good idea..
I haven't noticed that problem and have some sites that have hundreds of concurrent users. I'm interested in ways you have found for better performanance. Can you give more detail?
I don't know why, but Jcart is right. I've noticed that they have a tendency to hang around. For some reason, rather then being reused, they just become zombie like after a while sucking up resources.

On another note, my experience is that "Shared Nothing" refers more to a type of architecture and development strategy for Load Balanced clusters. The key thing to making this kind of strategy work is how a users session is managed. In the clusters i've dealt with (Turbo Cluster 6 and Cisco LD-420's), there is an option for making a session sticky. In other words, when a person connects, they are routed to the same box in the cluster with each new request. This all falls apart of course should there be a failure at the cluster manager level or an individual server.

We were working towards a two-tier approach that had sessions managed locally, but we could also write the session information to the database. Should the first become problematic, we could shift to the second.

There was also msession, but that seems to have disappeared. It was basically a server that could sit amongst a cluster of servers and provide session information to them. I was initially interested in '01 but it had no fault tolerance at the time. We decided to fall back to the DB approach.

Anyway, it works well for LAMP development but seems to throw a big question mark over the head of someone that is accustomed to data persistence.

Cheers
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

BDKR wrote:
arborint wrote:
Jcart wrote:Using pconnect on a medium to high volume website isn't really a good idea..
I haven't noticed that problem and have some sites that have hundreds of concurrent users. I'm interested in ways you have found for better performanance. Can you give more detail?
I don't know why, but Jcart is right. I've noticed that they have a tendency to hang around. For some reason, rather then being reused, they just become zombie like after a while sucking up resources.
pconnect is no good on shared servers either. Arborint, you may have found the one place pconnect is actually useful. I look forward to your paper detailing this discovery. :D

I thought the term to use was Stateless? Like HTTP. I thought "Share-Nothing" was a database term which yeah could apply to clusters.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

arborint wrote:
Jcart wrote:Using pconnect on a medium to high volume website isn't really a good idea..
I haven't noticed that problem and have some sites that have hundreds of concurrent users. I'm interested in ways you have found for better performanance. Can you give more detail?
The best resource for understanding the dangers (and potential benefits!) of pconnect is the story of what happened to yahoo, when they had a large number of users accessing a site driven using pconnect.

pconnect, by itself, isnt entirely evil. pconnect, using the defaults, without tuning the server to specifically support the choice to use them, can definitely be!

In situations where you cannot specify the mysql settings, pconnect is best to avoid. The defaults are simply too harsh. To be fair, there is no easy way mysql could include support for pconnect AND have it change the configuration for the other items when it is enabled. You can only have one "default".
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I could be alone here, and have the wrong end of things, but I dislike the idea of using a persistent connection on a user defined request application (ignoring AJAX).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Buddha443556 wrote:I thought the term to use was Stateless? Like HTTP. I thought "Share-Nothing" was a database term which yeah could apply to clusters.
I'm quite suprised that people do not associate "share-nothing" with PHP. Rasmus Lerdorf has been refering to it as such for a while:

http://www.oracle.com/technology/pub/ar ... _php.html]
http://itconversations.com/shows/detail58.html
http://techpatterns.com/forums/about567.html
http://www.zefhemel.com/archives/2004/0 ... chitecture
(#10850)
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

arborint wrote:
Buddha443556 wrote:I thought the term to use was Stateless? Like HTTP. I thought "Share-Nothing" was a database term which yeah could apply to clusters.
I'm quite suprised that people do not associate "share-nothing" with PHP. Rasmus Lerdorf has been refering to it as such for a while:

http://www.oracle.com/technology/pub/ar ... _php.html]
http://itconversations.com/shows/detail58.html
http://techpatterns.com/forums/about567.html
http://www.zefhemel.com/archives/2004/0 ... chitecture
To quote the "The Zen of Programming", "Suddenly the novice was enlightened."

I must admit I've missed 3 out of 4 of those interviews with Rasmus Lerdorf. Thanks.
:D
Post Reply