loading...

re: Elegant code in PHP VIEW POST

FULL DISCUSSION
 

I am a big framework user (Symfony since verison 0.67) but before explaining why a framework is needed for a decent size project I'll point out some errors in your code.

First you use short php tag, it's been more than 10 years that this is not a recommended practice. Even in the php manual they say you should not do this.

php.net/manual/en/language.basic-s...

First example of the code was working but later on you change the form method to "get" and this use the $_POST array to access your data...

Also you use this

include "BladeOne.php"; // our file downloaded from Internet.

Instead of using composer to manage your dependencies (would explain why you have difficulty to handle them). But let say do not want to use composer, the include should also be change for require_once since you don't want your code to continue if the file is not there and also make sure the file is not loaded twice in case your code get more complex and inclusion occur more than once (in another file for example). Also you relay of having the file in your include path which is another bad practice.

So this line of code should be something like

require_once DIR . '/BladeOne.php';

You do not validate correctly your input. By assigning $_POST['name'] to $name and then checking with if($name). By doing so you do not support a name with a 0 as if value since "0" == false. Obviously this is not a big issue considering we are expecting a "first name" and no one is call "0", but this last point go in the direction of why you should use a framework. One of the layer covered by a framework.

All that was to make a point related to:

It is exactly what I did, with the exception that, modesty apart, I am an awesome developer (but right now, I am more an entrepreneur).

Base on the point I made earlier I would not even hire as a intermediate developer.

To go on the point of not using or using a framework I wounder without a framework how would you do this:

  • You want a nice URL (aka modrewrite) how would map your url to the controller &
  • You need to access database (to read or write data)
  • You need more complex validation (more complex object structure)
  • You have a client and a dashboard (admin section) to manage entities
  • You have different view (html, json/xml for a api) how would reuse your controller logic if they are bound to a view ? (html in your case)
  • You need to put automation test (unit and/or integration), how would you proceed ?
  • You need to optimise some part of the code (mainly accessing the database) and you need a caching layer, how do you proceed ?
  • How would you document a REST api and keep the documentation up to date with the less amount of effort ?
  • How would you do performance optimisation and validate them ? (aka profiling)

I'll stop there for the question, I think I made my point.

But I agree with you, there is no perfect framework, and there is work involve with using one. But on the plus side, I was, by myself, able to support team of 6 front developers with the backend I was building and maintaining by myself and I would never have been able to do that without a framework.

I am use to work on project that have arround 100 tables 30 to 100 api endpoint, cron job, webhook (recurring payment from payment provider, mailsystem notification...) etc. Those are the kind of use case a framework bring a lot of values in a project. But if you have a small project use something like Silex.

I did my master project on PHP framework and what I have learn, don't build a in house framework unless it's you core business, instead find a great framework and build extension to it. Starting without a framework and adding libraries, methodology, structure, etc. If you project is big it's pretty much building a framework on the fly. You do this once and you would never do this again.

 

DELETE AT REQUEST OF DEV.TO because apparently, it is offensive. ¯(ツ)

Problem solved.

 

Again, a library, it is not a big deal. Also, both Doctrine and Eloquent are horrible. They work fine with a small set of information but they fail when the database reaches the +1 million rows.

Eloquent/Doctrine or whatever active record/ORM library you use has nothing to do with the database performance. If you're having trouble grabbing data from a database with over a million records, you're most likely dealing with an unoptimized database. And you'll definitely also run into performance issues using plain old sql.

For every point made, your solution is adding a library. You'd end up with a microframework of your own. One of the things you mentioned about the drawbacks of using a framework was that the team needed to be taught how to use it. But, it's even a bigger issue learning how to use an in-house framework with probably little to no documentation.

Laravel, for example, adds +600 classes for only for a single Hello World. Symfony does a better job, but not for much.

Real world applications are far more complicated than a "hello world".

Get that "frameworks are bad, too many classes/code" mentality out of your head. While I do agree frameworks are not the solution for EVERY need, they are great tools with usually lots of support/documentation available that we can use to our advantage.

Nobody that's going to work on a mid-size project is going to create their own ORM/Active Record library, validation library, etc.

Frameworks also offer the advantage of having a defined project structure, rather than having to figure out what a developer was thinking when creating his own microframework (where are X located? where does this come from?)

 

With all your listed points (and many more like working with filesystem, making and sending emails, queues, logs, image manipulations, translations, full multilingual support etc etc etc) I'm assuming the author would write his own libs. Or he would take an existing good one and trim it to fit his needs (which is absolutely fine in open-source world). But then - boom! - he would end up writing another framework :)

 

Been there! We started with a ‘microframework’ But ended up needing all the components of a full framework, gradually adding one at a time until that ‘boom’ moment! Then wishing we’d just used a proper framework in the first place 😂

code of conduct - report abuse