r/PHPhelp • • Jun 17 '19

Development in PHP without a framework / Mixing procedural and OO design in PHP

So as a fairly new person to web design using PHP, and still fairly new to it in general, I've decided to get my hands dirty and begin without any framework to really get a comprehensive understanding of what's really going on. The only other times I've dove into web development was in the Spark Framework for Java 8, where everything was pretty heavily abstracted. Its been a bit of a slog, but I feel like I'm slowly starting to understand the gist of things, and once I wrote a router everything began to flow a lot better.

So with my newness established, my real question is, the deeper I get into this, the more I feel like I'm going to have to essentially write an entire framework to properly adhere to Object Oriented Principles. It really seems inefficient (or at very least time consuming) to try to match web dev to pure OO, and I'm really tempted to leave a few choice areas practically procedural to make the entire project flow better. Its become a real nightmare particularly as I get further into separating my php from my html, ob_start() and ob_get_clean() have been the workhorses here, but I find them really clumsy to use.

Now I know I could just give up and switch to simply using a framework and a template engine, but I've found this whole process really educational, and I'd like to see it to the end. I've found it difficult to find resources and info to improve my design, most questions I search end up referring to using a framework. I'd really appreciate any thoughts and advice going forward!

8 Upvotes

7 comments sorted by

3

u/gui_ACAB Jun 17 '19 edited Jun 17 '19

I've done it before and ended up with a simple framework i really enjoyed.

What helped me at the time was to check others frameworks like Slim and debug them. By debug i do mean set a breakpoint at the top entry level and go from there.

I've learned how to properly apply design patterns because they were needed.

Giving an example. The Hydrator pattern i believe I've checked from Doctrine. I didn't had the need of it while i coded in csharp but it's really usefull in PHP.

1

u/TheHeenie Jun 17 '19

Thanks for the response, I'll definitely try to look at some preexisting simple frameworks! I've never even heard of the Hydrator pattern lol, is it basically a specialized way to go about serialization?

1

u/gui_ACAB Jun 17 '19 edited Jun 17 '19

I've seen people using it to convert an object to array and vice versa.

I've used in the ORM library. Basically i get the data from DB in a array and then I have objects, let's call them entities. The hydrator factory creates one hydrator per entity (this is done once and updated when the entitires are modified) and when it does it with reflection it gets all properties from the entities and maps them with the keys of the array returned from the DB.

So those hydrators created per entity receive the array and populate the entity with the data.

You can apply the same pattern in the router. If you use DTOs for your controller actions request (a class named GetPosts, other UpdatePost) you fill that class with the data you get from the request extracted before.

Hope you can understand what I'm saying, English is not my native language.

0

u/[deleted] Jun 17 '19

[deleted]

1

u/gui_ACAB Jun 17 '19

Yeah i get you. Usually i stick with Slim because its awesome and even a poor skilled dev can learn it in a few days with their docs.

I've used Laravel before but i regretted as it would be much more productive and organised with ServiceStack but its also a different language.

2

u/halfercode Jun 17 '19

In general, I would say that if you have some solo projects, use your own framework. I agree there is a good learning opportunity here.

However, if you have any projects where someone else will maintain them after you, don't practice on your colleagues - use a tried-and-tested framework that is still in security/feature maintenance.

1

u/taoyx Jun 18 '19

Make sure you understand how an autoloader works.

0

u/gsogso111 Jun 17 '19

Write you own framework. It's not a hard to do.

All you need is knowledge about MVC pattern, namespaces and PSR (0,1,2,4)

First you have to lean how to use composer and how to write composer.json. Other way is to write own autoloader

In my sites i use my own framework, it is a simple, lightweighted and very fast. Of course, i know symfony/laravel etc, but i think that is a hard and slow frameworks.

Do your own framework