Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Three tier PHP system

Question posted by: hsriat (Expert) on May 15th, 2008 11:11 AM
Is there anyone who codes PHP in three tier?

If you know anything about that, can you please send me links of some tutorials?

I'm not able get good performance with the simple procedural coding style, so need to change the code to three tier (although its not going to be a week's job).


Thanks
coolsti's Avatar
coolsti
Needs Regular Fix
295 Posts
May 15th, 2008
12:18 PM
#2

Re: Three tier PHP system
An interesting question, I look forward to reading the replies.

But I am curious what you mean by not achieving good performance. Can you elaborate? Where do you think your bottlenecks are?

Reply
hsriat's Avatar
hsriat
Expert
1,472 Posts
May 15th, 2008
01:10 PM
#3

Re: Three tier PHP system
Quote:
An interesting question, I look forward to reading the replies.

But I am curious what you mean by not achieving good performance. Can you elaborate? Where do you think your bottlenecks are?


Well, the major problem is scalability, which have been a big issue in my case since I started testing. And then the limited number of mysql connections. I get the error "Warning: Too many connections" quite frequently, when 50-100 users test it concurrently. And after some research, I have come to know that for the sites which could have many number of concurrent users, its recommended to adopt the three tier design.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 15th, 2008
10:06 PM
#4

Re: Three tier PHP system
Quote:
Well, the major problem is scalability, which have been a big issue in my case since I started testing. And then the limited number of mysql connections. I get the error "Warning: Too many connections" quite frequently, when 50-100 users test it concurrently. And after some research, I have come to know that for the sites which could have many number of concurrent users, its recommended to adopt the three tier design.


I must be quite ignorant, I thought 3 tier design was just html/php/mysql combination? But it sounds like you are using mysql, and you probably have html and php with that so, I'm a little lost. I know I'm not contributing to the solution, but I might be able to if I can research it myself.

Regards

Reply
hsriat's Avatar
hsriat
Expert
1,472 Posts
May 16th, 2008
05:20 AM
#5

Re: Three tier PHP system
After some googling, I found this example. But still need some good tutorials.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 16th, 2008
06:14 AM
#6

Re: Three tier PHP system
Quote:
After some googling, I found this example. But still need some good tutorials.


I still think that's what I do in my php code. I have the normal page, with include()'s around the top in the most appropriate order (my order might be different tho). So I still don't understand how that will stream line your database calling. Have you optimized all your MySQL variable calling? Like only calling the specific variables needed for that page? You said that you had limited connections? If you have 100 users, all accessing the database, regardless of how you code, you will have 100 requests (and 100 connections - for each session)?

Reply
hsriat's Avatar
hsriat
Expert
1,472 Posts
May 16th, 2008
07:50 AM
#7

Re: Three tier PHP system
Even I don't know how's that.
That's why searching for some tutorials.

Its good for you at least you could understand the code. I couldn't even do that. I've never used object oriented php. But I think I should learn it. Don't you think its better to use oop then procedural one?

Reply
dlite922's Avatar
dlite922
Site Addict
564 Posts
May 16th, 2008
08:11 AM
#8

Re: Three tier PHP system
Quote:
Is there anyone who codes PHP in three tier?

If you know anything about that, can you please send me links of some tutorials?

I'm not able get good performance with the simple procedural coding style, so need to change the code to three tier (although its not going to be a week's job).


Thanks


Three-tier, by which i assume you mean MVC ... Model View Controller.

Large applications absolutly demand it. Luckily PHP 5 came through and all my applications are in MVC.

I've created my own framework, which i find that i have more controll over vs. say zend.

I highly suggest you design your own framework and experiment, then move on to a framework such as zend framework or things like this:
http://www.phpmvc.net/

CakePHP is also another great one.

For your database connection issue, what you can do is instead of connect() use pconnect()

p = persistent.

This way php/mysql do no create more connections, if the connection parameters are the same, it looks for a current one and uses it.

An e-commerce site is the minimum you should use MVC for, otherwise, you'll have overhead. Its just unnecessary for small projects.

Steps you should take:
1. Read up on MVC (wikipedia) and IBM have great info
2. Understand PHP classes and objects thoroughly (public, private, static, etc)
3. Create your own MVC framework.

To walk you through it, let's for example say i have a module in my app that manages customers and needs the CRUD functions. (Create, Read, Update, Delete)

You create a DAO class. (Data Access Model) Call it CustomerDAO.php

Then create a controller class, call it CustomerCT.php

then create a customer Class, call it CustomerVO.php (Value Object)

and finally you'll have a regular php file that will orchestrate and use the controller class.

This php file will basically manage the actions. You read POST, see what action the user wants (add, delete, view, update) by reading POST vars.

Then put those four funtions in a switch statement. Then call your (already instantiated at this point) conroller.

$customerCT->getCustomer($_GET['customer_id']);

that should return a customerVO object, which then you can pass to your smarty like so

$smarty->assign("customerVO",$customerVO);

and parse the customer info in Smarty!!!

4. Pad yourself in the back for your first MVC class.

If you need code to start from, I can email you some. (DAO is the toughest to get just right)

Good luck,


Dan

Reply
Atli's Avatar
Atli
Moderator
2,531 Posts
May 16th, 2008
08:54 AM
#9

Re: Three tier PHP system
As I understand it, all PHP applications would be considered Tier-Three applications... At least if based on Wikipeida's definition of it.

The static content sent to the client's browser would be the 'Presentation Tier'.
The PHP code itself, doing all the back-end server stuff would be the 'Application Tier'
And the DBMS, whether it is MySQL or something else, would be the 'Data Tier'.

Am I wrong here?

Reply
Markus's Avatar
Markus
Moderator
2,219 Posts
May 16th, 2008
09:26 AM
#10

Re: Three tier PHP system
Quote:
As I understand it, all PHP applications would be considered Tier-Three applications... At least if based on Wikipeida's definition of it.

The static content sent to the client's browser would be the 'Presentation Tier'.
The PHP code itself, doing all the back-end server stuff would be the 'Application Tier'
And the DBMS, whether it is MySQL or something else, would be the 'Data Tier'.

Am I wrong here?


Sounds right to me.

And OOP is a MUST.

I started learning it a week ago, just from online tutorials and I'm still finding it hard to grasp =/

MVC complicates me something rotten, so I shan't bother with it until I'm happy with my OOP.

*subscribing*

Reply
hsriat's Avatar
hsriat
Expert
1,472 Posts
May 16th, 2008
09:33 AM
#11

Re: Three tier PHP system
Quote:
Three-tier, by which i assume you mean MVC ... Model View Controller.
.....................

Good luck,

Dan

Hey! Thanks for that explanation.

I'll read all the stuff out on the website you told me. If I couldn't get it, then will ask for the code.

:)

Regards,
Harpreet

Reply
hsriat's Avatar
hsriat
Expert
1,472 Posts
May 16th, 2008
09:35 AM
#12

Re: Three tier PHP system
Quote:
As I understand it, all PHP applications would be considered Tier-Three applications... At least if based on Wikipeida's definition of it.

The static content sent to the client's browser would be the 'Presentation Tier'.
The PHP code itself, doing all the back-end server stuff would be the 'Application Tier'
And the DBMS, whether it is MySQL or something else, would be the 'Data Tier'.

Am I wrong here?

No that's not the case. It's a bit different. I also used to think it this way, but not now.

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
May 16th, 2008
11:21 AM
#13

Re: Three tier PHP system
Also have a peek at this thread Layers and Tiers

Ronald

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 16th, 2008
12:51 PM
#14

Re: Three tier PHP system
I know it wasn't my question, but tanks for all the responses, you learn something new every day! Hmmm, *nervously looking at OOP* I guess if marcus said it's a must, I should give it a go :S

Reply
dlite922's Avatar
dlite922
Site Addict
564 Posts
May 17th, 2008
09:56 AM
#15

Re: Three tier PHP system
Quote:
I know it wasn't my question, but tanks for all the responses, you learn something new every day! Hmmm, *nervously looking at OOP* I guess if marcus said it's a must, I should give it a go :S


Learn Java. Find some tutorials and execute some classes using textpad, which has a compiler and a good editor to keep around to use instead of notepad.

Java is the like first grade for OOP.

Reply
Atli's Avatar
Atli
Moderator
2,531 Posts
May 18th, 2008
01:26 AM
#16

Re: Three tier PHP system
Quote:
Learn Java. Find some tutorials and execute some classes using textpad, which has a compiler and a good editor to keep around to use instead of notepad.

Java is the like first grade for OOP.

It would probably be easier to just learn OOP in PHP tho.
Learning all the Java eccentricities would probably just confuse the issue.

But if your going to learn a whole new language, I'd go with C#... The free Visual Express editor is veeeery helpful when your starting out.

Reply
Markus's Avatar
Markus
Moderator
2,219 Posts
May 18th, 2008
10:02 AM
#17

Re: Three tier PHP system
Quote:
It would probably be easier to just learn OOP in PHP tho.
Learning all the Java eccentricities would probably just confuse the issue.

But if your going to learn a whole new language, I'd go with C#... The free Visual Express editor is veeeery helpful when your starting out.


Noted.

Doing that now :D

Reply
hsriat's Avatar
hsriat
Expert
1,472 Posts
May 18th, 2008
10:14 AM
#18

Re: Three tier PHP system
Quote:
It would probably be easier to just learn OOP in PHP tho.
Learning all the Java eccentricities would probably just confuse the issue.

But if your going to learn a whole new language, I'd go with C#... The free Visual Express editor is veeeery helpful when your starting out.


I would use PHP OOP rather than trying out any product of Microsoft.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 18th, 2008
11:26 AM
#19

Re: Three tier PHP system
Thanks for the advice, the next thing on the list will be OOP in PHP. I do want to learn javascript, but I wana nail PHP so I can get everything started. Basic javascript works for me now.

Reply
realin's Avatar
realin
Familiar Sight
249 Posts
May 18th, 2008
12:41 PM
#20

Re: Three tier PHP system
i agree with all of above ..
I am using smarty and i am happy with its performance, but i get to know about mysql_pconnect here, that was a real great thing to know. I was worried about n requests at a time from the clients, cause the site i am working on has already 40,000 above members..

Smarty really lets you concentrate on code rather than cosmetic woes...

Talking about java Vs. C#, i will stick to java or php, but wont ever touch a MS technology.. I hate MS

Reply
hsriat's Avatar
hsriat
Expert
1,472 Posts
May 18th, 2008
12:50 PM
#21

Re: Three tier PHP system
Quote:
I hate MS


Do you use windows?

Reply
realin's Avatar
realin
Familiar Sight
249 Posts
May 18th, 2008
12:51 PM
#22

Re: Three tier PHP system
Quote:
Do you use windows?


My bad (i use at office), but at home i am on UBUNTU :P

Reply
Atli's Avatar
Atli
Moderator
2,531 Posts
May 18th, 2008
02:08 PM
#23

Re: Three tier PHP system
Quote:
Talking about java Vs. C#, i will stick to java or php, but wont ever touch a MS technology.. I hate MS

I don't have much affection for M$ either, but I have to give them credit for C#. They did a good job on that one.

Reply
coolsti's Avatar
coolsti
Needs Regular Fix
295 Posts
May 20th, 2008
07:58 AM
#24

Re: Three tier PHP system
Its been a while since I saw this thread but many thanks to the contributors. I will use myself the links mentioned to see "how things are supposed to be done" :)

I myself have developed a very clear manner of structuring my scripts, reading intended actions from POST and with switch statements then calling the appropriate PHP scripts and then subscripts, and instantiating the appropriate object classes, making for very easy to read and efficient code. But I have done this all ad-hoc, and now I look forward to seeing the mistakes I may have made :))

I agree that people working with PHP might as well use PHP for learning OOP. The only disadvantage with PHP's OOP is that there are not many strict rules (if you ignore PHP5 warnings) which I take advantage of in my scripts. But there is no real need to learn "something else" like Java in order to learn enough OOP to get going in PHP. On the other hand, Java or C++ are good ways to learn proper OOP programming.

Reply
hsriat's Avatar
hsriat
Expert
1,472 Posts
May 20th, 2008
08:07 AM
#25

Re: Three tier PHP system
Quote:
I myself have developed a very clear manner of structuring my scripts, reading intended actions from POST and with switch statements then calling the appropriate PHP scripts and then subscripts, and instantiating the appropriate object classes, making for very easy to read and efficient code. But I have done this all ad-hoc, and now I look forward to seeing the mistakes I may have made :))


Same here. I too don't let a single line in the code unmanaged and inappropriate. All with proper code comments. And still I believe its not the best.

OOP is must. I use procedural in both PHP and JavaScript, and it works wonderful (according to my clients). And when I see someone else's code with proper use of OOP, for a while I loose my confidence.

Actually I was originally a C programmer, programming embedded systems. So never used OOP. But now really need to switch over to it.

Thanks for all the replies.

Harpreet

Reply
dlite922's Avatar
dlite922
Site Addict
564 Posts
May 20th, 2008
08:51 AM
#26

Re: Three tier PHP system
I'm telling you guys, PHP will not teach you OOP they way Java or C++ does.

If you're programmed procedurally in any language, its very easy for you to pick up another.

I'd assume you'd go through a C++ book easily.

How to Program C++/Java by Deitel and Deitel is a the best book i've read and is almost standard in most colleges.

I just found this too, haven't read it, but looks like you can use it to get your feet wet:

http://www.zib.de/visual/people/mueller/Course/Tutorial/tutorial.html

Reply
coolsti's Avatar
coolsti
Needs Regular Fix
295 Posts
May 20th, 2008
09:50 AM
#27

Re: Three tier PHP system
Well, heh heh, I am a chemical engineer with a varied background, but have spent a lot of time in my career programming in everything from APL to Fortran to C/C++ to Java and of late PHP. I agree that PHP will not be a good language to really learn OOP, but I am wondering how many who are totally involved in PHP programming would like to have to learn Java or C++ just to learn OOP, despite that the education will be all the better.

I have spent the last hour reading the links shown in this thread (hitting myself in the head wishing I read this stuff a few years back). I don't use templates, all my html is produced with PHP, but I can see the advantage to separating the code into scripts that produce the browser output, code that reads the database, and code that does the business part of the application. In the future I will try to do this.

However, I have also started to read the documentation on this phpMVC stuff, and although it sounds like a fantastic system, trying to do what I am doing using phpMVC would maybe be like trying to force a round peg into a square hole. But I would consider trying to use it if I were to start a new application again from scratch.

It is funny to me that the applications I have written with PHP during my learning of this language have migrated to a structure similar to that used by phpMVC, where I have all requests directed to one and only one main.php file, and use switch statements based on POST variables to determine where the chain of command will go to next. This is the sort of delegation used in the phpMVC framework. Only I have not made any attempt to separate code that affects the database from other code.

But I have a very intricate HTML view system that emulates a tabulated view (which remembers what was "opened" on all tabs when the user navigates from one tab to another by remembering the POSTed variables for that tab) and I do not see how this would fit into the phpMVC framework.

It is, however, nice to read about these techniques, as I will use what I can and it will definitely improve my programming style in the future.

By the way, a very very old book nowadays, but a good one and entertaining reading if someone wants to learn C++, with emphasis on OOP: Thinking in C++ by Bruce Eckel. There is also a Thinking in Java which I believe also is excellant.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 20th, 2008
10:12 PM
#28

Re: Three tier PHP system
Quote:
I am a chemical engineer...


HEY!!! So am I! Graduated last year though, so a little less experienced!

Reply
hsriat's Avatar
hsriat
Expert
1,472 Posts
May 25th, 2008
11:28 AM
#29

Re: Three tier PHP system
[DELETED]

Created new thread.

Reply
Reply
Not the answer you were looking for? Post your question . . .
190,182 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top PHP Forum Contributors