Discuss Is this well written php? in the PHP Development forum on Dev Shed. Is this well written php? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 1,843
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 32 m 15 sec
Reputation Power: 813
Hi,
I find it great that you want to learn and improve your code. You really have a lot of ambition.
But don't ask for our blessing whenever you write a line of code.
Your code is fine, so no need to worry about having done something "wrong". Yeah, maybe one of the "PHP gurus" around here still finds something he doesn't like if he looks really, really hard. So what? Would that make your code bad or your users unhappy?
You've obviously understood the MVC pattern, the rest is just details. And there isn't a definite "right" in "wrong" in programming, anyway.
Don't get me wrong: It does make sense to ask more experienced programmers about their opinion on a specific idea or solution. But don't expect a forum to do your daily code review. That's just too much.
However, I do have some remarks regarding the templates:
Smarty does not free you from escaping values. When you just use {$var}, you'll have the same security problems like with <?php echo $var ?>. You always have to e. g. apply the "htmlentities" modifier:
Code:
{$var|htmlentities}
It's also possible to define default modifiers that will be applied to all variables automatically.
And you should avoid the cumbersome "section" syntax (unless there's a special reason why you need it). Use the "foreach" syntax instead.
Last edited by Jacques1 : November 24th, 2012 at 01:12 AM.
Posts: 1,589
Time spent in forums: 2 Weeks 4 Days 19 h 37 m 37 sec
Reputation Power: 70
Quote:
Originally Posted by Jacques1
Smarty does not free you from escaping values. When you just use {$var}, you'll have the same security problems like with <?php echo $var ?>. You always have to e. g. apply the "htmlentities" modifier:
Code:
{$var|htmlentities}
It's also possible to define default modifiers that will be applied to all variables automatically.
Umm, if you see the the controller in my code above, I was using htmlentities there in my controller. Which brings us to another discussion: Is it better to use htmlentities in controller or in template?
I find it great that you want to learn and improve your code. You really have a lot of ambition.
But don't ask for our blessing whenever you write a line of code.
Your code is fine, so no need to worry about having done something "wrong". Yeah, maybe one of the "PHP gurus" around here still finds something he doesn't like if he looks really, really hard. So what? Would that make your code bad or your users unhappy?
I wasted months doing what I thought was the right way. There was a point I realized there are many ways things can be done in php but a lot of those methods could be wrong. I am self taught and havent done schooling on this, I realized checking here saves me loads of time. Remember you told me about Smarty?
Quote:
Originally Posted by Jacques1
Also I don't see why you would use this oldschool PHP/HTML spaghetti code for your templates when at the same time you do all this fancy OOP to get a clean structure. I'd use a template engine like Smarty or Twig.
It is adding a whole new skill sets to my list. Now when I look for php jobs I see Smarty I know what it's talking about.
Quote:
Originally Posted by Jacques1
And there isn't a definite "right" in "wrong" in programming, anyway.
O man,, there is. Trust me. I have done some code out of ignorance and wrong assumptions you won't believe
Quote:
Originally Posted by Jacques1
But don't expect a forum to do your daily code review. That's just too much.
I've almost always got something out of every thread I posted. I really don't see much harm. But sure, I hear you and try to post only if I have to.
Posts: 1,843
Time spent in forums: 1 Month 2 Weeks 1 Day 7 h 32 m 15 sec
Reputation Power: 813
Quote:
Originally Posted by zxcvbnm
Umm, if you see the the controller in my code above, I was using htmlentities there in my controller.
OK, that one htmlentities() I didn't see. But they should be applied to any value that goes into the template, regardless of whether or not it's actually "dangerous" right now.
Quote:
Originally Posted by zxcvbnm
Which brings us to another discussion: Is it better to use htmlentities in controller or in template?
I'd put it in the templates, because then you can actually see if every value is escaped. It's easier to forget that in the controller.
But if you want to do all the data preparation in the controller and keep the templates "stupid", you might as well put it in the controller.
Quote:
Originally Posted by zxcvbnm
Quote:
Originally Posted by Jacques1
And there isn't a definite "right" in "wrong" in programming, anyway.
O man,, there is. Trust me. I have done some code out of ignorance and wrong assumptions you won't believe
Well, I did exaggerate a bit. Of course there are many problems where one solution is definitely better than another. But when you've reached a certain level of programming techniques and begin to deal with design details, often enough there is no "best solution". One approach might be slightly faster, the next one is a bit more readable etc. -- so it basically depends on one's own preferences
Regarding the htmlentities() question, for example, I think that's really a matter of taste.
Quote:
Originally Posted by zxcvbnm
I've almost always got something out of every thread I posted. I really don't see much harm. But sure, I hear you and try to post only if I have to.
I'm really not suggesting that you should post less often or something. I just got the impression that you sometimes worry too much about doing something "wrong".
Posts: 1,589
Time spent in forums: 2 Weeks 4 Days 19 h 37 m 37 sec
Reputation Power: 70
Quote:
Originally Posted by Jacques1
OK, that one htmlentities() I didn't see. But they should be applied to any value that goes into the template, regardless of whether or not it's actually "dangerous" right now.
I'd put it in the templates, because then you can actually see if every value is escaped. It's easier to forget that in the controller.
But if you want to do all the data preparation in the controller and keep the templates "stupid", you might as well put it in the controller.
Well, I did exaggerate a bit. Of course there are many problems where one solution is definitely better than another. But when you've reached a certain level of programming techniques and begin to deal with design details, often enough there is no "best solution". One approach might be slightly faster, the next one is a bit more readable etc. -- so it basically depends on one's own preferences
Regarding the htmlentities() question, for example, I think that's really a matter of taste.
I'm really not suggesting that you should post less often or something. I just got the impression that you sometimes worry too much about doing something "wrong".
Posts: 3,415
Time spent in forums: 3 Weeks 5 Days 9 h 21 m 40 sec
Reputation Power: 3833
I'd omit this fluff
PHP Code:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
as your files seem to only contain classes then direct access to the script would cause no execution at all
If PHP was some how disabled then the raw source would be viewable whether that line was in or not - because that line wouldn't run because PHP was disabled.
a cleaner way, IMO, is to keep library files outside the web root