Learning php in 2022 — Vol.2

David Boschmann
3 min readJul 25, 2022

--

3 Cups of Coffe with nice milk patterns
Photo by Emma Smith on Unsplash

What can you do with php?

Last time we investigated the latest changelogs of php, like jumping into cold water. And when jumping a lot of questions bubbled to the surface. Today I’m trying to answer some of these but presumably we are going to get more of them.

When it’s about to the question “What can you do with something?” you usually look at what are people doing with this it. So lets do this for php…

I knew that nextcloud was developed using php. Actually I heard several times… “Look php isn’t dead… the whole nextcloud is build on it”. So, what can you do with php? Here is an example: nextcloud/server: ☁️ Nextcloud server, a safe home for all your data (github.com)

About 61% of this about 2.6GB sized repository is php code. So there should be plenty of stuff to learn (and it tells a lot if you not count the lines of code but rather the Gigabytes).

The first look into the nextcloud Repo

On the very first look I saw a .devcontainer folder, which I like. This is actually a Visual Studio Code feature that allows you to specify your dev environment using a docker container, so that e.g. newcomers in the project (like me) just need to build this docker container and have all the tools and build dependencies set up. Not only is the setup done properly, it is also documented perfectly in a simple Dockerfile:

FROM ubuntu:focal # we start with ubuntu...# ...and install PHP. Not the latest version...
RUN apt-get update -y
RUN apt-get install --no-install-recommends -y \
php7.4 \
# ...and some other stuff

…and the second one

Actually the Dockerfile looked nice, but unfortunately something was misconfigured in the devcontainer.json there. However this is not stopping me from looking into the code. Lets pick a random php file and try to understand it. As Nextcloud is about files I’m going with apps/files/simplelist.php:

<?php 
/// TODO: move to handlebars
$config = \OC::$server->getConfig();
$userSession = \OC::$server->getUserSession();
$showgridview = $config->
getUserValue($userSession->getUser()->getUID(),
'files', 'show_grid', false);
// renders the controls and table headers template
$tmpl = new OCP\Template('files', 'simplelist', '');
// gridview not available for ie
$tmpl->assign('showgridview', $showgridview);
$tmpl->printPage();

Ok we have this <?php introduction at the very beginning. Show this is a php file to the interpreter i guess. Variables are defined with a $sign prefix. Php uses -> syntax to access methods and :: double colons for namespace navigation (at least I guess). This \OC:: looks like a unfamiliar syntax, especially with the starting backslash. There we have our first question generated. And also there is another backslash between in OCP\Template(...)

What does a \ mean in front of a namespace?
What is the difference of \ and :: ?

As the backslash reminds me of some kind of a root folder, lets take a look at the root folder of the project and see what we can find there. Et voilà there is a composer.json and a composer.lock file. This looks very similar to package.json and package-lock.json files in npm javascript setups. In that json file there is a section called autoload which maps OC to the folder lib/private. So we discovered a way of dealing with imports in php and reusing code from different places.

What does autoload do, and how to use it with php?

Some more insights

I discovered php files that look mostly like html, just with some php tags in between. I guess these are dynamically replaced once the file is executed.

It also looks like when starting to dig from the index.php — assuming this is the entrypoint — that the actual server.php file is a pretty huge thing wiring up hundrets of other files into one single thing. I see there are services registered to some Container, and later there are callbacks defined that listen to some Requests or Commands. I guess there is some Framework involved here…

Next time we’ll take a look on some php frameworks / libraries.

--

--