The different ways in which different projects use PHP code

Status
Not open for further replies.

Mat8iou

Ars Praefectus
5,394
Subscriptor
I'm sure this is a problem with all code, although some languages give a lot more structure than others, which probably reduces the problem.

Today, my query / rant is about PHP code in open source projects. I'm not a programmer day to day, but I do tweak things on a regular basis in a fair number of languages at one time or another (qbasic originally, nowadays mostly Lisp, Python and PHP. What strikes me is how differently different projects approach similar problems. Templating, Writing modules (plugins, extensions, whatever you want to call them), modifying ad extending the software takes completely different paths in each project.

I can understand that conceptually different projects approach things in different ways and that many large projects have experienced complex birth processes. However, I feel that when I move from one project to another I'm having to re-learn far more than I should need to in terms of how they approach things, how they decide to save customisations etc.

Examples that I'm basing it on (at this stage) would include Wordpress, Drupal and Suite CRM (a Sugar CRM fork). They all use a common programming language, but from there onwards, the approach to everything seems so different. Surely there is scope for modularising projects far more - to take some work off the mundane operations (such as updating the core - something that follows a different process in each of the above examples) and leaves programmers free to work on the more interesting aspects of it.

I like to imagine that there is some progress in this field and to an extent there is with e.g. common libraries for Wysiwyg editors such as FCK etc. There still seem to be far more differences than common ground however in the way things are approached.
 

Jehos

Ars Legatus Legionis
55,560
What you're describing isn't a feature of a language, it's a feature of software engineering. The language doesn't dictate much about how to architect a software project. Imagine architecting buildings--deciding to use PHP is kind of like deciding to use wood. You still have to decide are you building a house? A church? A library? How many stories is it? Should the stairs be in the front, back, or middle? How many windows does it have?

Software design is the same. Just because you're using PHP doesn't mean there are any standards about what you're building. For example where should user preferences live? On the server or in client cookies? Are you authenticating users against a database? A directory service? Facebook? Making those decisions is the real meat of programming work, and different teams decide on different things for different reasons.
 

Ardax

Ars Legatus Legionis
19,795
Subscriptor
It's not, really.

If a particular development system has effective, useful, dependency management, then you'll see some of the pain alleviated, since 3rd party dependencies all get shoved into it. (e.g. Nuget, NPM, CPAN, etc.) Sufficiently sophisticated might have 1st party code bundled into packages and put into the dependency system (or a localized version of it) as well.

While PHP has PEAR, I don't think it's seen nearly the level of uptake that other systems have. And I'm not sure it's available at a granular enough level. It's been a long time since I've had to deal with PHP.

Once you're back into 1st party code, though, the same hurdles still exist.
 

Jehos

Ars Legatus Legionis
55,560
Another consideration is that .Net (for example) has Microsoft putting a whole lot of engineering and then marketing behind the "right" way to build apps for any given release of technology. For example let's say you want to build a MVC web app in C# with ASP.Net and Razor. You'll probably use some flavor of Visual Studio, which means you'll start with a template that does the MVC plumbing for you and you choose how you want to do things like user authentication. You'll probably use Bootstrap for your layout since it's in the template. You'll probably use Knockout for your view models since it's in the template. You chose how you wanted to authenticate users from a very short list of options when you set up the project, and your choice is baked into the template. That whole experience is designed by Microsoft to make it easy to build apps the "right" way and hard to build them the "wrong" way.

PHP on the other hand doesn't have any central source of tools and methodologies to define the "right" way to do things. Anybody can fire up vim or emacs and start hacking away on code, reinventing all the wheels as they go. So naturally you'll see more variations in wheel design in a LAMP app.

Now lest I be accused of fanboyism, Microsoft seems to redefine the "right" way to write apps every couple years. I'm pretty sure it's due to the culture of people switching jobs internally after a product is released, methodologies being products, and PMs getting to make a name for themselves by designing a new thing rather than improving an old thing. So at Microsoft the wheel reinventions are more spaced out and follow a release schedule, but they still get reinvented.
 

GohanIYIan

Ars Legatus Legionis
10,196
I think this is possibly the issue - Most stuff only uses the standard Pear libraries if at all - possibly because most web hosts don't like installing additional ones for you.

From there onwards, any modularity is through integration of third party modules, each of which is written in its own way.

Composer has largely supplanted PEAR as the package manager of choice for PHP. Drupal 8 uses it extensively. Wordpress doesn't use it at all. This fits with how the projects have behaved historically. Wordpress has typically been very conservative and slow to change, where as Drupal will aggressively overhaul and refactor in new major releases. IMO for long-running projects like these this attitude toward change is a huge source of differences in style and structure.
 

hestermofet

Ars Legatus Legionis
25,464
What you're describing isn't a feature of a language, it's a feature of software engineering. The language doesn't dictate much about how to architect a software project.
Totally. It's called a language for a reason: you can express yourself however you want, to say/create anything you want. Just like different authors have different writing styles, different developers and different projects have different programming styles.

I understand what you are saying - but at least from what I see, the problem seems far worse with PHP than it is with other languages. Its hard to quantify this properly, but its the impression I get.

Some languages are more "opinionated" than others: that is, they prefer you write with particular idioms. A good example of this is Python which is, by intention, heavily opinionated. The Python project's maintainers intend you to write Python the "right" way. The "right" way being code that is easily readable.

PHP doesn't expect anything out of people who use it. You're free to make beautiful things in an elegant way, or horribly ugly things with hacky spaghetti code. The "worst" language I've seen in this respect is R. In any normal language, you can say some variant of this:
Code:
a = 1 + 2

In R, you can say it like this:
Code:
a = 1 + 2

Or you can say it like this:
Code:
 a <- 1 + 2

And if you want to get really wild, you can even say it like this:
Code:
1 + 2 -> a


All three of these lines do exactly the same thing. :facepalm: (there are subtle differences with the last two, but those differences are not seen in most situations). For even more madness, you are free to include or not include a line-terminating semi-colon, it's up to you. So that's 6 ways to do a simple assignment operation.

For some R coders, this:
Code:
john.height
Means a variable within the variable of "john" that stores John's height. For others, it means a method within the class "john" to calculate John's height. There's no standard naming convention either way. There are two kinds of classes in R, the normal type of class you're familiar with where the methods belong to the class, and then there's the historically older S3 style, where the class belongs to the method (a "generic" method determines which class to use). :eek:

edit: I just looked this up, there's actually a third style of class that I wasn't aware of. :rolleyes:

If you think PHP is bad, R is built on a foundation of sand. The language grew by accretion with a bunch of non-programmers solving problems they faced in the most expedient way, not the most elegant. Their solutions were then added to the base language or the package repositories. Wheels get reinvented all the time in R, and many packages I've come across have values that are hard-coded for the particular data set that the author wrote the package for. Even for things like file paths.

You can obviously write spaghetti code in Python, but it's designed (in theory) so that this is harder to do. R practically encourages spaghetti code. :)

I guess you could say R (and PHP) is like simple Lego blocks. It's up to you how to put the pieces together, but this means you can end up with a car that has 9 wheels, 3 doors, and a hat. Python is like a Lego Death Star. It has enough pieces so that you could make whatever you want, really, but it does expect that you'll make a Death Star. The benefit to the Python way of doing things is that it's a lot easier to make something really complex like a Lego Death Star with a Lego Death Star set than it is with simple bricks.
 

hanser

Ars Legatus Legionis
43,853
Subscriptor++
I understand what you are saying - but at least from what I see, the problem seems far worse with PHP than it is with other languages. Its hard to quantify this properly, but its the impression I get.
PHP is kind of a lowest common denominator language. I have found that there seems to be more cargo cult nonsense with PHP (that's often wrong, c.f. SQL injection vulnerabilities, mysqli usage, etc) than with other languages.

WRT web services, the PHP runtime is also quite a lot different than something like Java. You set up and then tear down an enormous amount of stuff to serve a web request. With compiled languages, you often have one or more processes that live for long periods of time. That can lead to different architectural patterns, especially around, e.g., caching.

In some sense, you're describing the idioms associated with a programming language + problem space. Idioms are stochastic, and often take a little while to emerge, undergo refinement, and settle. In the case of PHP, it perpetually seems like it's just had its Tower of Babel moment.
 
Java, .Net, Objective-C/Swift, etc, all have a single company exposing particular methods. While those are not really the only company they are seen as the primary driver of the language and therefor they have significant influence on design architectures. Languages like PHP don't generally have that framework to build upon. And truth is, some of the worst examples of how to write PHP come from the organization responsible for PHP.

PHP was built to solve a problem. It was rebuilt and expanded to solve another problem. A few quintillion iterations later and we have a plethora of design ideas.
 

ramases

Ars Tribunus Angusticlavius
9,024
Subscriptor++
I understand what you are saying - but at least from what I see, the problem seems far worse with PHP than it is with other languages. Its hard to quantify this properly, but its the impression I get.
PHP is kind of a lowest common denominator language. I have found that there seems to be more cargo cult nonsense with PHP (that's often wrong, c.f. SQL injection vulnerabilities, mysqli usage, etc) than with other languages.

It's not only the lowest common denominator language, it is also the lowest common denominator environment.

One of the "biggest" advantages of the PHP runtime is its ubiquitousness and easy accessibility. Whatever else they may offer, every single garbage hoster at least has php support via mod_php, with marginally more useful hosters at least offering something like php-fpm.

So basically any "mass-market" php application is constrained to essentially what can be done within that environment or throw away one of the main advantages of the platform.
 
I understand what you are saying - but at least from what I see, the problem seems far worse with PHP than it is with other languages. Its hard to quantify this properly, but its the impression I get.
PHP is kind of a lowest common denominator language. I have found that there seems to be more cargo cult nonsense with PHP (that's often wrong, c.f. SQL injection vulnerabilities, mysqli usage, etc) than with other languages.

It's not only the lowest common denominator language, it is also the lowest common denominator environment.

One of the "biggest" advantages of the PHP runtime is its ubiquitousness and easy accessibility. Whatever else they may offer, every single garbage hoster at least has php support via mod_php, with marginally more useful hosters at least offering something like php-fpm.

So basically any "mass-market" php application is constrained to essentially what can be done within that environment or throw away one of the main advantages of the platform.

Some of the ubiquity is from the execution model. Shared hosting isn't the only affordable starting point anymore, but other software from the same era uses a similar shared-nothing setup with a CGI script running to handle each request (e.g., Movable Type in perl, Trac in python). Prior to VPSes becoming common letting users have long running processes was frowned on which ruled out application servers.

I don't know if anyone's tried to write a WSGI or Rack style PHP application server, but I doubt Wordpress or Drupal is going to change execution models anytime soon. Shared hosting is still fairly common, and setting up Wordpress or Drupal to run at pretty large scales is a solved problem.
 
Perl or Python is more likely to be there on a completely vanilla install. I wouldn't expect to find PHP on something that isn't an HTTP/HTTPS server and even then you're more likely to have Perl or Python if it's Rails or Tomcat+Spring or whatever non-PHP stack. I think the appeal of Bash is that if all you're doing is stringing together other CLI binaries Perl/Python/PHP require slightly more ceremony to invoke an external program. But yeah if you need functions or loops or any non-trivial conditional it's easier to use P* of choice.
 

Sunner

Ars Praefectus
4,994
Subscriptor++
Incidentally, PHP is a better shell scripting language than Bash. I don't know why sysadmins don't write their scripts in PHP. As has been pointed out several times here, php is pretty much on every Linux machine.

Sysadmins get started with bash for free since it's likely to be their daily driver anyways since it's the default shell for pretty much every major distro. And PHP is most definitely not installed on pretty much every Linux machine, the only case where that would be even remotely true would be environments that are heavily focused on something that actually requires PHP, hosting Wordpress or something for example. For "I need more than a quick bash script" Python is the go to language these days.
 

Jehos

Ars Legatus Legionis
55,560
Incidentally, PHP is a better shell scripting language than Bash. I don't know why sysadmins don't write their scripts in PHP. As has been pointed out several times here, php is pretty much on every Linux machine.

Sysadmins get started with bash for free since it's likely to be their daily driver anyways since it's the default shell for pretty much every major distro. And PHP is most definitely not installed on pretty much every Linux machine, the only case where that would be even remotely true would be environments that are heavily focused on something that actually requires PHP, hosting Wordpress or something for example. For "I need more than a quick bash script" Python is the go to language these days.
Yeah I'm confused by that too. You get neither PHP nor Python on a click-next install of Ubuntu Server, RHEL, Fedora Server, and (I'm assuming) CentOS. That covers the majority of your Linux systems. I don't know if you get it in check-all-the-boxes installs of Linux desktop distros, but that's hardly "pretty much every Linux machine".
 

Sunner

Ars Praefectus
4,994
Subscriptor++
Incidentally, PHP is a better shell scripting language than Bash. I don't know why sysadmins don't write their scripts in PHP. As has been pointed out several times here, php is pretty much on every Linux machine.

Sysadmins get started with bash for free since it's likely to be their daily driver anyways since it's the default shell for pretty much every major distro. And PHP is most definitely not installed on pretty much every Linux machine, the only case where that would be even remotely true would be environments that are heavily focused on something that actually requires PHP, hosting Wordpress or something for example. For "I need more than a quick bash script" Python is the go to language these days.
Yeah I'm confused by that too. You get neither PHP nor Python on a click-next install of Ubuntu Server, RHEL, Fedora Server, and (I'm assuming) CentOS. That covers the majority of your Linux systems. I don't know if you get it in check-all-the-boxes installs of Linux desktop distros, but that's hardly "pretty much every Linux machine".

I think all the major distros ship Python by default in their "standard" selections these days. In Redhat you can't even remove it unless you're doing some really custom things since many of the core tools including the installer are written in Python. And with Debian (and I would assume Ubuntu) it's in the "standard system utilities" or whatever it's called tasksel group. So I think it's fair to say Python is installed on "Pretty much every Linux system", if it's not you'll probably know about it and why.

Oh and if you want to have fun, compile and install Apache, mod_php and OpenSSL on an old piece of junk Solaris box. :facepalm:
 
D

Deleted member 43669

Guest
Incidentally, PHP is a better shell scripting language than Bash. I don't know why sysadmins don't write their scripts in PHP. As has been pointed out several times here, php is pretty much on every Linux machine.

... what? Any rationale for that?

Bash is pretty good at small scripts- most scripts cobble together other command line tools, and Bash does that very well- heck, pipes and command execution are pretty much superior to any other non-shell language I can think of.

For bigger scripts, Python/Ruby/Perl are probably the way to go. Nicer ecosystems for scripting, and much better languages in general.
 

hanser

Ars Legatus Legionis
43,853
Subscriptor++
I find Bash's syntax to be pretty esoteric. OTOH, PHP is more C-like, like the languages I use on a daily basis. I also find PHP's error handling more robust, because it's a "real" programming language.

If I was more sysadmin than developer, I might think differently.

Bash is pretty good at small scripts- most scripts cobble together other command line tools, and Bash does that very well- heck, pipes and command execution are pretty much superior to any other non-shell language I can think of.
The ML family of languages would like a word with you. You can construct similar data processing pipelines in C# using LINQ as well.
 
D

Deleted member 43669

Guest
Bash is pretty good at small scripts- most scripts cobble together other command line tools, and Bash does that very well- heck, pipes and command execution are pretty much superior to any other non-shell language I can think of.
The ML family of languages would like a word with you. You can construct similar data processing pipelines in C# using LINQ as well.

Well, actually I was referring more about stuff like:

Code:
$ diff /etc/passwd <(ssh other.host cat /etc/passwd)
16,18d15
< polkitd:x:999:997:User for polkitd:/:/sbin/nologin
< postfix:x:89:89::/var/spool/postfix:/sbin/nologin
< chrony:x:998:996::/var/lib/chrony:/sbin/nologin
20,21d16
< centos:x:1000:1000:Cloud User:/home/centos:/bin/bash
< puppet:x:52:52:Puppet:/var/lib/puppet:/sbin/nologin

The whole piping/standard UNIX utilities combo is pretty awesome, and replicating that in most other languages is a lot more effort- some languages might have similar facilities for terse data flow, but many don't have the same terse UNIX-utility-invoking mechanism. For *scripting*, it's hard to beat. Stuff such as https://amoffat.github.io/sh/ are very promising, though- you could really do something like that in most dynamic-ish languages and probably challenge UNIX shells.

I do find the concepts of Powershell intriguing, though. It's a "cleaner", more "robust" and "structured" thing, but UNIX shells have a huge head start, plus OpenSSH is really awesome- shells, SSH and UNIX utilities work very well together.

I also find PHP's error handling more robust

Mayyyybee (but I really wouldn't touch PHP with a bargepole, much less for scripting, so I can't offer any meaningful critique), but really in the kinds of scripts where I find bash appropriate, "set -ue" is more than enough.
 
I've worked with PHP for a long, long time. It's tiresome.

My big beef with PHP applications - not scripts, which no one really uses, but apps - is that they don't get compiled. You just can't squeeze the same performance out of them that you could with Elixir or Go.

Where this really gets to me is with people using PHP for web services, especially when using one of the content management systems mentioned in the OP. They are very slow compared to compiled applications. Feels like they are designed for the sites that get a few hundred visitors daily and don't care about latency.

Try troubleshooting a millisecond problem on a PHP app, or squeezing it for a few hundredths of a second. You can't.
 

Mat8iou

Ars Praefectus
5,394
Subscriptor
I've worked with PHP for a long, long time. It's tiresome.

My big beef with PHP applications - not scripts, which no one really uses, but apps - is that they don't get compiled. You just can't squeeze the same performance out of them that you could with Elixir or Go.

Where this really gets to me is with people using PHP for web services, especially when using one of the content management systems mentioned in the OP. They are very slow compared to compiled applications. Feels like they are designed for the sites that get a few hundred visitors daily and don't care about latency.

Try troubleshooting a millisecond problem on a PHP app, or squeezing it for a few hundredths of a second. You can't.

In a lot of cases, the sites involved get in the hundreds of visitors per day range, so this probably works fine for 95% of installs.

Is the reason for avoiding compiled languages that it takes a bit more effort to install or that they would require different compiles for different web servers? I guess that the advantage with PHP in this area is that most of the time (at lest with the popular projects) the installs are pretty simple - enter a few DB access details and apart from that you are generally up and running without much else needing to be done.
 

Mat8iou

Ars Praefectus
5,394
Subscriptor
Incidentally, PHP is a better shell scripting language than Bash. I don't know why sysadmins don't write their scripts in PHP. As has been pointed out several times here, php is pretty much on every Linux machine.

... what? Any rationale for that?

Bash is pretty good at small scripts- most scripts cobble together other command line tools, and Bash does that very well- heck, pipes and command execution are pretty much superior to any other non-shell language I can think of.

For bigger scripts, Python/Ruby/Perl are probably the way to go. Nicer ecosystems for scripting, and much better languages in general.

I have to admit that I've never considered using PHP for scripting - Python / Ruby seem more geared up for this sort of thing and there are far more resources out there explaining how they can be used for scripting tasks.
 
Status
Not open for further replies.