views:

224

answers:

9

Hi, 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.

+1  A: 

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

Good luck!

Galwegian
+1  A: 

This will be perhaps too harsh, but: don't consider making an own framework unless you fully understand the object oriented programming. The OOP knowledge will evolve from your procedural programming when you gain more experience.
Creating a PHP file that contains your commonly used functions is a nice idea though and I'd say go for it. But don't call it a framework :)

dark_charlie
And what's the best way to gain knowledge? To make mistakes (since you can therefore learn from them). And what's the best way to make mistakes? To try to do something outside of your scope of current knowledge (just outside)... So what's the best way to understand OOP? To write OOP! Building something like a framework can be a great tool for learning how OOP works... Too many people IMHO just know how to use a framework (like Zend or whatever), not how and --more importantly-- **why** it works... A little bit of experimentation and failure can go a long way to gaining experience...
ircmaxell
@ircmaxwell You make a good point, but keep in mind it can be tricky to learn from your mistakes. You can write something that compiles and does what you want it to do, but that doesn't mean there aren't any mistakes in there
GSto
@ircmaxell: Yes, that's what I meant by "evolving from the procedural programming". I also didn't say "don't go for it", I just said "don't call it a framework".
dark_charlie
+1  A: 

Admirable as it may be if you don't even know what the basic fundamentals of building a framework are, especially patterns, and are not fully grounded in OOP and comfortable writing it then how do you expect to achieve your goal? You will not necessarily learn much by writing code yourself, i.e. if you write bad code 40 times it is not teaching you a thing (this is not a personal criticism of you by the way) but by reading other peoples code, which will be both good, bad and indifferent then you will learn.

PurplePilot
+1  A: 

Usually, a Framework has a specific goal. A Framework might support a CMS, a 3D engine, a data-access system, etc... you may often see several frameworks used together to accomplish the ultimate goal, such as using Spring, Hibernate, and JavaEE to build a Java-based web application.

From the look of it, you are collecting all of your assorted favourite functions into a single file (or do you group them into different files by functionality?). This can be useful, but I would not call this a framework - yet. It's really a library. If all of your functions are intended to support fancy email functionality, then learn OOP, and build a framework whose single goal is to provide an easy-to-use interface to fancy email functionality.

FrustratedWithFormsDesigner
I am planing to group them into a single file for ease of use.
hart1994
+12  A: 

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.

Galen
+1 I agree, use this project as practice to learn OOP. Then even if you decide to use other frameworks instead of your practice one, you'll know OOP and you'll know how to use those frameworks better.
Bill Karwin
I think using an established framework is a great way to hone your OOP development skills by seeing firsthand how they can better be applied to web development.
webbiedave
i agree but you must first understand the underlying concepts
Galen
I did something similar. I wrote a website for my own use as a way to learn using PHP and utilised OOP. Eventually I ran into a wall when wanting to add functionality - it was too painful with my home grown stuff. But then I looked at Zend and realized that what I had been trying to do was to build a framework. After that I dumped all my home grown stuff and knuckled down to learn MVC. I think that you don't really appreciate a framework until you to try to reinvent one badly!
Peter M
+1 Very true. Also, after you write your framework you'll have a new appreciation for discovering how other frameworks solve similar problems.
Mike B
+1  A: 

I would also bet on another framework. There are simple ones with large communities. You could help yourself in three ways: start with many contributed modules/good structure, read high quality code and get support from a large community.

Here there is a list with many popular frameworks and comparison - phpframeworks.com. I could also recommend you CodeIgniter - good for beginners, pretty simple and MVC based at the same time.

Mario Peshev
+2  A: 

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.

webbiedave
Don't forget CodeIgniter!
chigley
@chigley: added it.
webbiedave
Another one worth considering: Kohana: http://kohanaframework.org/
GSto
@GSto - problem with Kohana is the lack of consistent documentation. If you can get around that hurdle, I'd say it outperforms CI as it doesn't provide back-support for PHP4.
chigley
@chigley, true, but there are a lot of other resources out there, such as the unofficial Kohana wiki: http://kerkness.ca/wiki/doku.php. CodeIgniter beats it in simplicity and documentation, but I feel Kohana is a lot more feature rich, and I like the HMVC pattern it uses.
GSto
+1  A: 

Others have answered your framework question (only build something for the learning value, otherwise learn to use one of the better existing mature framework and libraries) but I just want to point out a small nit with your code, the whole point of having a sigil $ before the var is that you can easily do

$headers  = "From:$from\r\n";

instead of

$headers  = "From:";
$headers .= $from;
$headers .= "\r\n";
Pat
For some strange reason it was giving me exactly that, a "$from", instead of the actual data stored inside the $from.
hart1994
maybe you used single quotes instead of double quotes in your test?
Pat
+2  A: 

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.

mr. w