Jump to content

Archived

This topic is now archived and is closed to further replies.

andygrant

My 1st PHP OOP site, the performance seems slow.

Recommended Posts

Hi

 

I have just built my 1st php site using objects but it seems to be much slower than usual sites I've built.

 

I've used __autoload to load classes, and foreach a lot more as opposed to while loops are these things slow? Or could it just be thats its using a lot more memory by holding arrays of objects compared to just records of mysql results.

 

I found it better to code with OOP but if its going to be slower I'm not sure I can continue this way.

 

Any ideas will be apprieciated.

 

Thanks

Share this post


Link to post
Share on other sites

What version of PHP are you working with?

 

Also it could very well be "how" you implemented OOP. Just because you coded it and it works, does not mean you are implementing/using OOP in it's proper manner.

 

OOP Methodology is a completely different line of thinking from traditional programming.

Share this post


Link to post
Share on other sites

What you described about your code isn't very helpful.  It all depends on your design and how you optimize your code.  Here's a quick list of little things you can do to speed things up: 63 + Best practices.  I only skimmed through, but it seems pretty accurate.

 

You should also read about proper design models and optimizations for OOP.  There are plenty of tutorials.

Share this post


Link to post
Share on other sites

What you described about your code isn't very helpful.  It all depends on your design and how you optimize your code.  Here's a quick list of little things you can do to speed things up: 63 + Best practices.  I only skimmed through, but it seems pretty accurate.

 

You should also read about proper design models and optimizations for OOP.  There are plenty of tutorials.

 

Thanks for the link, this will help me out as well....

Share this post


Link to post
Share on other sites

My classes aren't particulally complicated but I guess the OOP technique I used issues more SELECT statements than before.

 

Basically all my classes are created using an id, then they do a SELECT using that id and loop through the fields populating each variable in the object. If a classes extends another class they each do SELECTS as well, rather than if I did a single SELECT accross multiple tables at once the old way.

 

So if I did a search for 'news items' it would get the id's for each news item and create each news object using the id, and each object would do a SELECT to get the full details.

 

I'm not sure how else to do it really, does this sound like a poor technique?

Share this post


Link to post
Share on other sites

I do OOP Programming, and while it can be a bit cumbersome to begin with -- it gets easier with time.  As does anything you're new to.

 

I personally also use Smarty to seperate my HTML from my logic...

 

Within smarty there is a function called section, which is simlar to php's foreach...

 

So I created a function to manipulate my database information in the query -- Easier to do 1 select and get everything at once...  Here is the function I wrote, hope it helps you to get on your way:

 

<?php

function sql_md_array($query, $cnt) {
for ( $row = 0; $row < $cnt && $array = mysql_fetch_assoc($query); $row++ ) {
foreach ($array as $key => $value) {
  $mda[$row][$key] = $value;
  }
}
	return $mda;
}

?>

 

Again I wrote this to fit with the way the section needs the array to be in order to have it work correctly -- but hopefully this will work for you as well..

 

Then for the SQL query I would use just something like SELECT * FROM table

 

then of course ya gotta count it if you use the type of code i've written..

 

Hope this helps a little?

Share this post


Link to post
Share on other sites

My classes aren't particulally complicated but I guess the OOP technique I used issues more SELECT statements than before.

 

Basically all my classes are created using an id, then they do a SELECT using that id and loop through the fields populating each variable in the object. If a classes extends another class they each do SELECTS as well, rather than if I did a single SELECT accross multiple tables at once the old way.

 

So if I did a search for 'news items' it would get the id's for each news item and create each news object using the id, and each object would do a SELECT to get the full details.

 

I'm not sure how else to do it really, does this sound like a poor technique?

 

I understand what you are saying. I am new to OOP as well. I hope someone can chime in on this as I'd like to know...

 

 

Share this post


Link to post
Share on other sites

 

 

I personally also use Smarty to seperate my HTML from my logic...

 

Within smarty there is a function called section, which is simlar to php's foreach...

 

 

Hi,

 

Would you say Smarty is the best templating engine to use? I am all for learning something new, and it seems everyone is going

towards some kind of templating engine or framework if they are serious about PHP development....

 

Just would like to get your thoughts, feel free to PM me if this gets too OT...

 

 

Share this post


Link to post
Share on other sites
I understand what you are saying. I am new to OOP as well. I hope someone can chime in on this as I'd like to know...

 

To put it simply. Yes, using objects can be slower on performance. Enough to worry about? No, the benefits of good design far out way the performance costs.

 

Would you say Smarty is the best templating engine to use? I am all for learning something new, and it seems everyone is going

towards some kind of templating engine or framework if they are serious about PHP development....

 

Now, if we want to talk about performance hogs, templating engines are where its at. Why add the overhead of a templating engine when you can separate your logic from html using the mvc design pattern. Yes, you still need minimal amounts of php within your templates, but it beats parsing some other mini language with php.

Share this post


Link to post
Share on other sites

I understand what you are saying. I am new to OOP as well. I hope someone can chime in on this as I'd like to know...

 

To put it simply. Yes, using objects can be slower on performance. Enough to worry about? No, the benefits of good design far out way the performance costs.

 

Would you say Smarty is the best templating engine to use? I am all for learning something new, and it seems everyone is going

towards some kind of templating engine or framework if they are serious about PHP development....

 

Now, if we want to talk about performance hogs, templating engines are where its at. Why add the overhead of a templating engine when you can separate your logic from html using the mvc design pattern. Yes, you still need minimal amounts of php within your templates, but it beats parsing some other mini language with php.

 

Thanks Thorpe!

 

Do you think that Smarty, or Zend Framework, CakePHP and all these frameworks are just a "trend"? I know I've spoken to some people who said

they just come up with their own MVC......

 

I've started using Aptana for coding PHP over UltraEdit, I still don't really know what the difference is all about other than Aptana being

a built in IDE. But I can't even get that to work right...

 

Sorry if I'm getting too OT...

 

Share this post


Link to post
Share on other sites
Do you think that Smarty, or Zend Framework, CakePHP and all these frameworks are just a "trend"? I know I've spoken to some people who said

they just come up with their own MVC......

 

Frameworks (smarty isn't one by the way) are not just a trend. For larger projects, if your not using one of the existing frameworks your going to end up pretty well writting your own anyway. May as well start with a good solid base.

Share this post


Link to post
Share on other sites

Do you think that Smarty, or Zend Framework, CakePHP and all these frameworks are just a "trend"? I know I've spoken to some people who said

they just come up with their own MVC......

 

Frameworks (smarty isn't one by the way) are not just a trend. For larger projects, if your not using one of the existing frameworks your going to end up pretty well writting your own anyway. May as well start with a good solid base.

 

Ok, one last question. Do you suggest any one over the other? The only 2 frameworks I'm aware of are Zend and CakePHP....

 

I'm still trying to understand the difference between a framework and a templating engine like Smarty...

 

 

Share this post


Link to post
Share on other sites
Ok, one last question. Do you suggest any one over the other?

 

There are a heap of php frameworks out there (see here) and its really just a matter of personal opinion. The only frameworks Ive looked at where symphony and zend. Zend IMO though is almost becoming the standard.

 

A templating engine is really just that. Nothing more. Some frameworks have templating engines built in, others will let you use something like smarty. But frameworks offer much more than just templating functionality.

 

Id suggest downloading and taking a look at zend. Especially if your just starting out with OOP it can be a big help though the learning curve may be a little steep.

Share this post


Link to post
Share on other sites

Ok, one last question. Do you suggest any one over the other?

 

There are a heap of php frameworks out there (see here) and its really just a matter of personal opinion. The only frameworks Ive looked at where symphony and zend. Zend IMO though is almost becoming the standard.

 

A templating engine is really just that. Nothing more. Some frameworks have templating engines built in, others will let you use something like smarty. But frameworks offer much more than just templating functionality.

 

Id suggest downloading and taking a look at zend. Especially if your just starting out with OOP it can be a big help though the learning curve may be a little steep.

 

 

Thank you again Thorpe!

 

I will do just that. You know what, I will probably just concentrate on Zend considering they are the creators of PHP anyways, I think the pro's and con's between any framework will probably go on forever....like Atom feeds VS RSS etc.....

 

Thanks again bro!

Share this post


Link to post
Share on other sites

Do you think that Smarty, or Zend Framework, CakePHP and all these frameworks are just a "trend"? I know I've spoken to some people who said

they just come up with their own MVC......

 

Frameworks (smarty isn't one by the way) are not just a trend. For larger projects, if your not using one of the existing frameworks your going to end up pretty well writting your own anyway. May as well start with a good solid base.

 

Ok, one last question. Do you suggest any one over the other? The only 2 frameworks I'm aware of are Zend and CakePHP....

 

I'm still trying to understand the difference between a framework and a templating engine like Smarty...

 

A couple of general comments:

 

1.  When dealing with performance you *MUST* have a tool that helps you profile.  As you already indicated, somehow you are doing more *queries*  which has nothing to do with OOP.  But in general,  you need to have some tool that lets you isolate things and do timings to know what is taking up time. 

 

2. Is PHP OOP slower than not procedural code?  Yes, although the overhead especially in PHP5 is marginal.  Would the use of Oop vs. non-oop be noticeable on a human perception level?  No. 

 

For most developers the reason to use OOP is that it lets you write better more reliable and functional code.  Often it would be near impossible to write and maintain a large non-oop system without global variables and spaghetti.  Not that oop is a panacea, but it does offer things specifically designed to make code modular.  I will make a comment on your use of autoload.  Autoload does not come without a price.  The price may be minimal, but if you don't absolutely need autoload, you are better off implicitly doing require() statements at the top of your scripts.

 

In regards to PHP/Mysql frameworks:

 

The big 3 right now in my opinion are:

-CakePHP

-Symfony

-Zend Framework

 

 

Each takes a different philosophy. 

 

Symfony was designed to provide and compete with the benefits offered by Ruby on Rails.  What it does it does really well.  It offers the most holistic package of features in a framework.  With that said, you really have to use it the way it was designed, and there's a lot of black magic in there.  People either love or hate the yaml configuration stuff for example.  We found there were some things that didn't work the way we wanted, and figuring out what to do when we hit the wall, involved just reading the code, and some of it wasn't very modular.  You have to invest time in looking at all the seperate projects they used to cobble it together and understand those individually.

 

CakePHP is the oldest of the three, and has a great community.  It does offer a lot of the same benefits as Zend Framework and symfony but it was originally developed in PHP4 whereas Symfony and Zend are PHP5 only.  It is the only framework that I don't have direct experience with, so I don't want to discount it out right without having tried it personally. 

 

Zend framework is the newest and is just starting to hit its stride.  It is the only one that had the design goal of offering a library of components as much as a framework.  Probably it's the one I would use right now, but it still is evolving.  Expect a learning curve that includes reading code.  The documentation is ok, but often will have an example that scratches the surface and really doesn't show you what you want to actually do with it.  Despite the "Comes from Zend"  (not really true, it comes from various people in the PHP community but was partially financed by Zend)  it does represent the best practices of PHP and PEAR.

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

In regards to PHP/Mysql frameworks:

 

The big 3 right now in my opinion are:

-CakePHP

-Symfony

-Zend Framework

 

 

Each takes a different philosophy. 

 

Symfony was designed to provide and compete with the benefits offered by Ruby on Rails.  What it does it does really well.  It offers the most holistic package of features in a framework.  With that said, you really have to use it the way it was designed, and there's a lot of black magic in there.  People either love or hate the yaml configuration stuff for example.  We found there were some things that didn't work the way we wanted, and figuring out what to do when we hit the wall, involved just reading the code, and some of it wasn't very modular.  You have to invest time in looking at all the seperate projects they used to cobble it together and understand those individually.

 

CakePHP is the oldest of the three, and has a great community.  It does offer a lot of the same benefits as Zend Framework and symfony but it was originally developed in PHP4 whereas Symfony and Zend are PHP5 only.  It is the only framework that I don't have direct experience with, so I don't want to discount it out right without having tried it personally. 

 

Zend framework is the newest and is just starting to hit its stride.  It is the only one that had the design goal of offering a library of components as much as a framework.  Probably it's the one I would use right now, but it still is evolving.  Expect a learning curve that includes reading code.  The documentation is ok, but often will have an example that scratches the surface and really doesn't show you what you want to actually do with it.  Despite the "Comes from Zend"  (not really true, it comes from various people in the PHP community but was partially financed by Zend)  it does represent the best practices of PHP and PEAR.

 

Great write up! Thank you, I definitely appreciate anyone taking the time to explain this stuff to me.

 

I've decided to go with Zend without much more research from CakePHP and the other one, Sympfony.

Maybe that's not a good idea but I will have to have faith that the people at Zend are going in the right direction.

Sometimes you just have to pick one and run with it....or you can go back and forth forever one a framework and never get good at either one of them.....

 

I don't mind the learning curve, I'll take it on full throttle...

 

Thanks for the write up!

 

 

 

Share this post


Link to post
Share on other sites

No problem, I think you will find it's easy to use, and I would recommend going with all aspects of it initially, including their templating.  Where I worked, we had a highly modified version of it, with Smarty built in, and a lot of the suppossed features of the thing were unusable due to all the hacking that went into it.

Share this post


Link to post
Share on other sites

No problem, I think you will find it's easy to use, and I would recommend going with all aspects of it initially, including their templating.  Where I worked, we had a highly modified version of it, with Smarty built in, and a lot of the suppossed features of the thing were unusable due to all the hacking that went into it.

 

I may come to you from time to time as I learn this framework. I hope you won't mind....

 

I have my PHP certfication test that I need to take in about 2-3 weeks. Once I get that out of the way, I can hit the Zend framework full throttle...

Share this post


Link to post
Share on other sites

Theres also a cert for zend framework that you might like to take in the future.

Share this post


Link to post
Share on other sites

Theres also a cert for zend framework that you might like to take in the future.

 

Yup, I saw that, it's probably WAY over  my head right now. Once I pass my PHP cert, I need to jump on my redhat cert which I kinda put on hold to do the PHP cert...

 

I think it would be a great idea to get Zend framework cert though....I don't really see employers asking for Zend certifications yet but it doesn't matter, these certs are for me and to help shore up my knowledge....

Share this post


Link to post
Share on other sites

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