Edit: grammar and spelling
Edit: grammar and spelling
Spaghetti code is not what PHP is infamous for. Spaghetti code can be written in any language. PHP is infamous for all library functions being in the global scope, lack of sane naming conventions for those things, terrible type conversions, unsafe libraries, starting php with every request, being able to drop raw php right into html templates - what it was initially designed for, and being just a general web programming language not much useful in any other context. It's also got a bad wrap for being the language of choice for people who do not wish to learn any other language or anything about computer science or computers.
PHP is what happened when people learned HTML and saw that once they got to forms they needed something else.
Love it or hate it, PHP will be around long after elitist devs turn their guns on noSQL and server side JS.
I don't know of many "template languages" that treat classes as first-class citizens, has an in-depth, thorough unit testing framework, traits, closures, and and and.
I'm sure you get my idea? PHP may have been much simpler at its inception - it is not the same beast now.
I hate when PHP pops up here on HN, 90% of the people flapping their gabs don't or haven't used PHP for many many years, and when they did it was some Indian's crap code so they spend their time talking shit about a language because they had to fix a bug in Hinderbar's PHP spaghetti from 2003.
Everyone is capable of writing crappy spaghetti code.
Basically it stipulates that you should keep classes outside of the main page loader to be compliant.
For example, say I want to plot a couple of data columns with gnuplot (an open source plotting program) into several images. I can write a PHP script "myplot.plt.php" that generates a gnuplot script (untested):
set terminal png enhanced
<?php
for ($i = 2; $i <= 4; $i++) {
$outputfile = "plot$i.png"
echo <<EOT
set output "$outputfile"
plot "data.txt" u 1:$i
EOT;
}
?>
and then invoke it from gnuplot with load "< php myplot.plt.php"
This is a stupid example, but it gets really powerful when you dynamically calculate labels, element positions, etc.. PHP may be a crappy language for web development, but it is a really nice macro language, and has really powerful string processing features, making it ideal for this kind of hack.Depending on your project, if most of your code lives in the front-end, using Laravel/PHP for an API back-end is a nice option these days. It all depends on what you are trying to accomplish and your available knowledge and resources.
Ever used C?
> lack of sane naming conventions for those things
Absolutely annoying but also fairly minimal. Mostly an issue in the string & array functions.
> terrible type conversions
Weak typing has to coerce somehow. Javascript is equally "terrible" at times.
> unsafe libraries
Such as? All the major libraries are thread-safe that I'm aware of
> starting php with every request
PHP exists outside of the web, and even in the web environment, you can run it as fastcgi since inception as a persistent daemon and php-fpm is the standard daemon now as well.
> being able to drop raw php right into html templates
I view that as a feature (which is often abused by beginners)
> Ever used C?
Come on, this is not a good argument. C is a 70's language, we can do better than that, even in 1995 when PHP came out.
>>> "0" == false true !!!!!!!
>>> "" == false true
>>> "" == "0" false
and here we lost the transitive relation...
That explains why its infamous for spaghetti code.
I've seen just as many good code bases as bad in pho.