Collection of commonly used PHP functions

Hi skunks, I've been working with PHP for a little while now and in that time, I've collected some of the functions I've written which I use in almost all of the projects I work on. I've cleaned them a bit and made them a more developer friendly, with generous comments. Maybe they can save someone a few hours of writing code, as they have for me. Any comments, criticisms, additions, modifications etc are welcome. Regards, Peter.

Hi Peter, Nice collection u got there i have a few concerns though. 1. Presentation is not seperated from logic. I have seen lots of <tr>'s , <td>'s and <selects> in the functions. This makes the functions not generic especially for some of us who never use tables for layout. 2. Printing out the mysql_error() is not a good idea, as it might expose your underlying db schema to the world. i would suggest you log the error in file as opposed to printing it out. 3. Interaction with the Data layer is not abstracted. Ur functions assume that DB will always be MYSQL, if e.g. u get a project that uses Oracle, it will mean that most of the functions will have to be rewritten. On Sun, Jul 26, 2009 at 4:16 PM, Peter Karunyu<pkarunyu@gmail.com> wrote:
Hi skunks, I've been working with PHP for a little while now and in that time, I've collected some of the functions I've written which I use in almost all of the projects I work on.
I've cleaned them a bit and made them a more developer friendly, with generous comments.
Maybe they can save someone a few hours of writing code, as they have for me.
Any comments, criticisms, additions, modifications etc are welcome.
Regards, Peter.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general

These are good codes as i find them easy to understand. Though you indicated you dint consider the idea of OOP, i think it would be better if they were in classes which we can extend instead of interfering in what you've already done or having to repeat the functions all over again in process of modification. On Sun, Jul 26, 2009 at 9:29 PM, Chris Mwirigi <mwirigic@gmail.com> wrote:
Hi Peter, Nice collection u got there i have a few concerns though.
1. Presentation is not seperated from logic. I have seen lots of <tr>'s , <td>'s and <selects> in the functions. This makes the functions not generic especially for some of us who never use tables for layout. 2. Printing out the mysql_error() is not a good idea, as it might expose your underlying db schema to the world. i would suggest you log the error in file as opposed to printing it out. 3. Interaction with the Data layer is not abstracted. Ur functions assume that DB will always be MYSQL, if e.g. u get a project that uses Oracle, it will mean that most of the functions will have to be rewritten.
On Sun, Jul 26, 2009 at 4:16 PM, Peter Karunyu<pkarunyu@gmail.com> wrote:
Hi skunks, I've been working with PHP for a little while now and in that time, I've collected some of the functions I've written which I use in almost all of the projects I work on.
I've cleaned them a bit and made them a more developer friendly, with generous comments.
Maybe they can save someone a few hours of writing code, as they have for me.
Any comments, criticisms, additions, modifications etc are welcome.
Regards, Peter.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general

@Chris, yes I agree, presentation should be separated from logic. I am yet to find a way to do this without having to use a templating system. My experiences with Smarty and Expression Engine were not amusing. As for the mysql_error(), i find it makes debugging faster but yes, I agree with you that it should never be used in an production environment. Question: Aint having errors logged to file sort of expensive in terms of performance? On the database abstraction, I am yet to work on an application of large enough scale to have that abstraction make sense. Besides, the likelihood of my clients changing databases sometime in the near future is very low. And being a performance freak, adding that additional layer doesn't sit well with me :-) In your own experience, which abstraction library would you recommend? @Felix, I have avoided the OOP feature of PHP like the plague! In my very uninformed opinion, I don't see much benefit of using OOP to justify the time used in learning it. However, that said, I will try to implement the same using classes and see how it goes. Thanks! On Mon, Jul 27, 2009 at 11:30 AM, Felix mwai <flxxmw@gmail.com> wrote:
These are good codes as i find them easy to understand. Though you indicated you dint consider the idea of OOP, i think it would be better if they were in classes which we can extend instead of interfering in what you've already done or having to repeat the functions all over again in process of modification.
On Sun, Jul 26, 2009 at 9:29 PM, Chris Mwirigi <mwirigic@gmail.com> wrote:
Hi Peter, Nice collection u got there i have a few concerns though.
1. Presentation is not seperated from logic. I have seen lots of <tr>'s , <td>'s and <selects> in the functions. This makes the functions not generic especially for some of us who never use tables for layout. 2. Printing out the mysql_error() is not a good idea, as it might expose your underlying db schema to the world. i would suggest you log the error in file as opposed to printing it out. 3. Interaction with the Data layer is not abstracted. Ur functions assume that DB will always be MYSQL, if e.g. u get a project that uses Oracle, it will mean that most of the functions will have to be rewritten.
On Sun, Jul 26, 2009 at 4:16 PM, Peter Karunyu<pkarunyu@gmail.com> wrote:
Hi skunks, I've been working with PHP for a little while now and in that time, I've collected some of the functions I've written which I use in almost all of the projects I work on.
I've cleaned them a bit and made them a more developer friendly, with generous comments.
Maybe they can save someone a few hours of writing code, as they have for me.
Any comments, criticisms, additions, modifications etc are welcome.
Regards, Peter.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general

Why OOP? Design is the answer. Being able to create a solution that meets a range of diverse requirements, you'll be need to have the bigger picture; move from this to the actual code. There is no efficient to achieving this than using OOP - I stand to be corrected. When carrying out general tasks, OOP might not be very important but to effectively deliver a solution, it's always good to be thorough. On separating the presentation from the logic, personally I applaud the smarty. It's as straight forward as PHP itself. It looked abstract to me at first but when I had to use it, I discovered it's not what I used to think; and I never looked back... On 7/27/09, Peter Karunyu <pkarunyu@gmail.com> wrote:
@Chris, yes I agree, presentation should be separated from logic. I am yet to find a way to do this without having to use a templating system. My experiences with Smarty and Expression Engine were not amusing.
As for the mysql_error(), i find it makes debugging faster but yes, I agree with you that it should never be used in an production environment.
Question: Aint having errors logged to file sort of expensive in terms of performance?
On the database abstraction, I am yet to work on an application of large enough scale to have that abstraction make sense. Besides, the likelihood of my clients changing databases sometime in the near future is very low. And being a performance freak, adding that additional layer doesn't sit well with me :-)
In your own experience, which abstraction library would you recommend?
@Felix, I have avoided the OOP feature of PHP like the plague! In my very uninformed opinion, I don't see much benefit of using OOP to justify the time used in learning it.
However, that said, I will try to implement the same using classes and see how it goes.
Thanks!
On Mon, Jul 27, 2009 at 11:30 AM, Felix mwai <flxxmw@gmail.com> wrote:
These are good codes as i find them easy to understand. Though you indicated you dint consider the idea of OOP, i think it would be better if they were in classes which we can extend instead of interfering in what you've already done or having to repeat the functions all over again in process of modification.
On Sun, Jul 26, 2009 at 9:29 PM, Chris Mwirigi <mwirigic@gmail.com> wrote:
Hi Peter, Nice collection u got there i have a few concerns though.
1. Presentation is not seperated from logic. I have seen lots of <tr>'s , <td>'s and <selects> in the functions. This makes the functions not generic especially for some of us who never use tables for layout. 2. Printing out the mysql_error() is not a good idea, as it might expose your underlying db schema to the world. i would suggest you log the error in file as opposed to printing it out. 3. Interaction with the Data layer is not abstracted. Ur functions assume that DB will always be MYSQL, if e.g. u get a project that uses Oracle, it will mean that most of the functions will have to be rewritten.
On Sun, Jul 26, 2009 at 4:16 PM, Peter Karunyu<pkarunyu@gmail.com> wrote:
Hi skunks, I've been working with PHP for a little while now and in that time, I've collected some of the functions I've written which I use in almost all of the projects I work on.
I've cleaned them a bit and made them a more developer friendly, with generous comments.
Maybe they can save someone a few hours of writing code, as they have for me.
Any comments, criticisms, additions, modifications etc are welcome.
Regards, Peter.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke
http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks
Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
-- Samuel Waithaka http://www.brighterdayweb.com http://www.linkedin.com/in/waithaka http://twitter.com/samwaithaka http://www.brighterdayweb.com/seo-articles.xml

On Sun, Jul 26, 2009 at 9:29 PM, Chris Mwirigi <mwirigic@gmail.com> wrote:
2. Printing out the mysql_error() is not a good idea, as it might expose your underlying db schema to the world. i would suggest you log the error in file as opposed to printing it out.
This reminds me of the annoying error that's frequently displayed on the top half of the Standard Newspaper online edition. (See attachment) Dunno why I constantly encounter it at around midnight. Anybody with access the Web-Admin?

"Too many connections"? That is one error I've never seen! Making an educated guess, it could be sorted by using mysql_pconnect() instead of mysql_connect() right? On Wed, Jul 29, 2009 at 1:35 AM, Tony Likhanga <tlikhanga@gmail.com> wrote:
On Sun, Jul 26, 2009 at 9:29 PM, Chris Mwirigi <mwirigic@gmail.com> wrote:
2. Printing out the mysql_error() is not a good idea, as it might expose your underlying db schema to the world. i would suggest you log the error in file as opposed to printing it out.
This reminds me of the annoying error that's frequently displayed on the top half of the Standard Newspaper online edition. (See attachment) Dunno why I constantly encounter it at around midnight. Anybody with access the Web-Admin?
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general

On Wed, Jul 29, 2009 at 11:07 AM, Peter Karunyu <pkarunyu@gmail.com> wrote:
"Too many connections"? That is one error I've never seen! Making an educated guess, it could be sorted by using mysql_pconnect() instead of mysql_connect() right?
Could be correct, since mysql_pconnect does not open a new connection if there's a similar connection already open.

Should work. This error occurs because each time a new user accesses a page, a new connection is created and never closed. Thus the max connections of MySQL is hit. On Wed, Jul 29, 2009 at 11:22 AM, Edwin Wambua <eddwak@gmail.com> wrote:
On Wed, Jul 29, 2009 at 11:07 AM, Peter Karunyu <pkarunyu@gmail.com>wrote:
"Too many connections"? That is one error I've never seen! Making an educated guess, it could be sorted by using mysql_pconnect() instead of mysql_connect() right?
Could be correct, since mysql_pconnect does not open a new connection if there's a similar connection already open.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
-- Regards, Brian Ngure

yep. mysql_pconnect() will sort it out. but seems that eastandard does not close their connections when done. causing the pile up On 7/29/09, Brian Ngure <brian@mystique.boldlygoingnowhere.org> wrote:
Should work. This error occurs because each time a new user accesses a page, a new connection is created and never closed. Thus the max connections of MySQL is hit.
On Wed, Jul 29, 2009 at 11:22 AM, Edwin Wambua <eddwak@gmail.com> wrote:
On Wed, Jul 29, 2009 at 11:07 AM, Peter Karunyu <pkarunyu@gmail.com>wrote:
"Too many connections"? That is one error I've never seen! Making an educated guess, it could be sorted by using mysql_pconnect() instead of mysql_connect() right?
Could be correct, since mysql_pconnect does not open a new connection if there's a similar connection already open.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks Other services @ http://my.co.ke Other lists ------------- Announce: http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks-announce Science: http://lists.my.co.ke/cgi-bin/mailman/listinfo/science kazi: http://lists.my.co.ke/cgi-bin/mailman/admin/kazi/general
-- Regards,
Brian Ngure
participants (7)
-
Brian Ngure
-
Chris Mwirigi
-
Edwin Wambua
-
Felix mwai
-
Peter Karunyu
-
Samuel Waithaka
-
Tony Likhanga