Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building own php framework

I am interested in building my own php framework for my personal use to make my coding life easier. I am doing this as I am fairly (sort of) use to php now, and can't seem to get use to any framework.

I have an idea of making loads of functions in a .php file. Like I have started to do, sor for the send mail function I have simplified it (for my use):

function sendmail($to, $message, $subject, $from){//USE sendmail($to, $message, $subject, $from)
$headers  = "From:";
$headers .= $from;
$headers .= "\r\n";
$headers .= "Reply-To:";
$headers .= $from;
$headers .= "\r\n";
$headers .= "X-Mailer: Drupal\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
mail($to, $subject, $message, $headers);
}

This will then be used in a contact form:

sendmail($_POST['to'], $_POST['message'], $_POST['subject'], $_POST['from']);

This mail function works for me.

However, I am not sure at all if this is correct to make a framework like this. I have looked into classes and objects for php but cant seem to understand them as there is no understandable/easy tutorial.

like image 518
ryryan Avatar asked Sep 29 '10 16:09

ryryan


4 Answers

People are going to tell you to not write your own framework, to use an existing one. Do not listen to them. It is a good learning experience and will help you understand the concepts which will make other frameworks make a lot more sense to you.

I personally needed to understand 2 things better before being able to use other peoples frameworks (and obviously write my own):

  1. OOP
  2. MVC

I spent days reading every OOP tutorial on PHP and every tutorial/wiki page on MVC. Then as a learning experience i wrote my own framework. Then i learned from my mistakes and I started from scratch and wrote another framework. I probably wrote 5 versions. Then i decided to try out code igniter. After all the reading and practicing i finally understood it.

Since then i have only been using other peoples frameworks.

like image 172
Galen Avatar answered Sep 30 '22 07:09

Galen


Like some others here, I see only positive gains from an inexperienced user attempting to write a framework. If they're looking to existing options as models, and actually attempting to use the new code and thus identify its weaknesses with the goal of fixing them, it can be a great way to rapidly develop knowledge. That said, for a very new user, I would maybe think twice about using it in a production application; then again, it's probably not going to make much difference if the application's core code is written by the same user.

Having said that, a framework is very architectural by its nature and thus perhaps not the best place to get started. A simple library of utility code is much better, and is exactly what the OP is doing (terminology issues aside). Good for him.

As to always jumping on the existing framework bandwagon when it comes time to get serious, I have deeply rooted reservations about that. First of all, there's no perfect framework, or even any framework that's more than marginally good for every purpose. Most of the general-purpose frameworks are overly complicated performance sloths compared to code hand-tuned for its purpose. Thus, for an experienced team working on complex, real-world applications, a GP framework will often be a bad idea. This is why when it comes to such frameworks, I prefer those, like Zend, that let you cherry-pick the functionality you need without having to jump in with both feet.

More critically, in the ~30 years I've been developing software, I've seen many frameworks, even ones with close to 100% market saturation and the backing of major vendors, simply die. When this happens, developers are stranded. And no, being open-source doesn't alleviate this problem. If it takes many experienced people years to develop and then maintain a large framework, how is a small team inside a company--often with only one or two really experienced people--supposed to realistically maintain the project when it falls out of favor and starts to die? This happens with smaller code projects, too: witness the death state in which many once-popular PEAR libraries now find themselves.

like image 37
mr. w Avatar answered Sep 30 '22 07:09

mr. w


It may be of use to look at this simple php framework by Tyler Hall for some ideas.

Good luck!

like image 25
Galwegian Avatar answered Sep 30 '22 08:09

Galwegian


If you want to use a framework for personal use, you should use one of the established, open source offerings such as CakePHP, symfony, Zend or CodeIgniter. These frameworks have been developed and tested for years by talented web developers and will most likely more than meet your needs. The only time you should make your own framework is for educational purposes or if the existing frameworks do not suit your requirements. No need to reinvent the wheel.

From wikipedia:

The framework aims to alleviate the overhead associated with common activities performed in Web development. For example, many frameworks provide libraries for database access, templating frameworks and session management, and they often promote code reuse.

That's exactly what these frameworks aim to do and are quite successful at it.

By using these, you will also learn to appreciate their solutions as well as understand how they use OOP for web development, increasing your knowledge as a developer.

like image 36
webbiedave Avatar answered Sep 30 '22 07:09

webbiedave