Cover image for Elegant code in PHP

Elegant code in PHP

jorgecc profile image Jorge Castro Updated on ・1 min read

DELETE AT REQUEST OF DEV.TO because apparently, it is offensive.


Problem solved.


markdown guide

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.


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.


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 😂


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?)


That’s the path (almost) every junior dev goes through - write shitty and insecure spaghetti php mixed with html, then write own kind-of-lib resembling existing well-tested tools for the sake of “being lighter”, then abstract some things away and eventually end up with an unmaintainable pile of junior-level code. Then learn to do it properly with proven tools and finally understand why all those middle/senior devs recommended to learn a framework in the first place.


DELETE AT REQUEST OF DEV.TO because apparently, it is offensive. 🤷

Problem solved.


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).

I wasn't talking about you specifically, more in general. However, calling yourself an awesome developer with such a low-quality code (see @mpoiriert 's comment above) sounds like a red flag.

what you call it proven tool

It ain't perfect of course, but does a great job for a great number of projects.

I found that a lot of code hasn't been changed in years

This isn't necessarily a bad thing.

And no, seniors do not use frameworks

Oh, they do and they don't. Frameworks are just tools. What puts senior devs apart is a skill of choosing the right tool for the job.

PHP doesn't have a standard

If you mean a "standard framework" then you're missing the entire point. There are different frameworks - different tools for different jobs. That's absolutely fine.

Sloan, the sloth mascot Comment marked as low quality/non-constructive by the community View code of conduct

I wasn't talking about you specifically, more in general. However, calling yourself an awesome developer with such a low-quality code (see @mpoiriert 's comment above) sounds like a red flag.

I am better than you and that's important.


I'm sorry but it seems like you never worked on a serious project :)


DELETE AT REQUEST OF DEV.TO because apparently, it is offensive. 🤷

Problem solved.


Too many basic errors in the code, too much assumptions, too much radical claims maybe? :)

DELETE AT REQUEST OF DEV.TO because apparently, it is offensive. 🤷

Problem solved.

you must show proof


you don't want to show yourself as an END-USER

Couldn't care less.

no proof

There's plenty of proofs in these comments.

lots of complaints and whines

None. Just facts.

Man, don't take it hot and personally. Your other code on GitHub is ok, it's not a personal attack on you, by any of the commenters here including myself. But this particular article with these specific code fragments is rubbish. Sorry.

DELETE AT REQUEST OF DEV.TO because apparently, it is offensive. 🤷

Problem solved.


What is elegant with this legacy and bad practice coding? I would remove this article at all especially after all this comments people wrote. this is not elegant or anything else, this is how all beginners write PHP, you literally showed us how junior you are, that's all. I'm really sorry for this comment, but this is more like spam for me seeing this kind of posts.


My code for this:


$doc = new \DOMDocument('1.0', 'UTF-8');
$name = $_POST['name'] ?? null;
$data = new \DOMElement('data', $name);
$xslt = new \XSLTProcessor();
$template = new \DOMDocument();
echo $xslt->transformToXml($doc);

For $name=="<script>alert('Hello')</script>"; will output:

<div>&lt;script&gt;alert('Hello')&lt;/script&gt; is nice name</div>

How will look your output?


Hello there,

Quite a few folks have brought some information to your attention to indicate that what you're claiming is incorrect, and you've given some responses that are rather unsavory.

I suggest reviewing the Dev.to Code of Conduct.

With regard to the post itself: I think that something constructive could be salvaged from it, if it were sufficiently reworded.

I worked with PHP for about six years, and agree that older versions of PHP are flawed, in the sense that there are no strict safeguards when it comes to common bugs that sneak into code bases. These issues are logic based and definitely more frequent when one is learning a language. However, the language is still very popular and being improved consistently. That includes the frameworks that senior engineers are developing and using, in order to mitigate the human error that is inherent in our field. Frameworks are incredibly powerful tools. They allow us to maintain consistent standards. They do this by raising the overall quality of systems while lowering the overheard of developing and maintaining a completely in-house architecture.


in your answers to every comment, you say that you can use a library or class to do so: isn't that what is Frameworks is all about?

You say that SEO is fine with ugly URLS: what about all Google SEO url friendly recommendations and how crawlers are working.

I think you should read more and learn more before you write this article, I suggest you remove it because it is misleading information and can seriously be harmful for junior developers to read.


Suppressing errors is really absolutely the wrong method to teach. Why not $name = $_POST['name'] ?? false;


DELETE AT REQUEST OF DEV.TO because apparently, it is offensive. 🤷

Problem solved.


Right now, I am using PHP 7.3 and it is a PITA to target most of my code to PHP 5.6

You are not, in any form, obligated to target <7.0. In fact, you're doing a disservice to a language and community by still supporting non-supported and outdated versions. And with all that legacy support baked in your code suddenly becomes much less "elegant".

For example, one problem if what if the user sends other malicious information, for example, over-flow. PHP self-protects against it (limit of the post) but what if the information will be stored in the database (even if we use prepared statements). So we must add more restriction to this value.

There are solid and proven validation libraries out there, most frameworks have them built-in. That's one of the reasons frameworks exist :)

However, and for this basic example, the code is safe.



Suppressing errors, regardless of PHP version, is a practice that should not be taught! It's highly discouraged, and for a good reason.


Oh god, php with html, i thought this way of using php was in the past or in legacy projects.


Well, I do feel like frameworks add overhead to our apps, but must of the time they tend to put us in the path of good practices and organize our work (& our mind) A LOT.

When I started developing on PHP I used to feel that everything was messy, un-organized, until well stablished frameworks, workflows and patterns came to fill the void.

I personally think that if you are new to a language, after the basics you should dive into some framework that will lead to good practices.

Any way, thanks for putting the time, and the pics 😉


ur dum

this comment was purposely meant to convey an equivalent amount of quality as your blog post.


Please, can you elaborate more on your inquiry because it lacks information?.


@if, @else , @endif - they are components of your new imperative program language. Use PHP instead, it will better.

How you will make recursive template for presentation tree?