Tag Archives: PHP

PHP reflection API fundamentals

Do you ever take the time to read the code of your favourite PHP framework or library? If you have, there’s a good chance that you might have seen them use PHP’s reflection API. And, if you don’t read other people’s code, you should start doing it! (It’s a great learning tool.)

Now, let’s get back to the reflection API. This is an API that gets used quite a lot by framework and library developers. But the funny thing is that it’s almost never talked about when you’re looking at resources to learn PHP. (Or even advanced PHP resources for that matter.)

This is unfortunate. That’s because PHP’s reflection API serves a unique and important purpose. That’s why developers that build our frameworks and libraries use it so much.

Continue reading PHP reflection API fundamentals

How does a PHP application work?

Most of us are familiar with how a web application works. We know that when we enter an URL in our browser, it’ll contact a web server who’ll return some HTML. The browser will then make follow-up requests and then render the web page. (This is a simplification! But that’s the main idea.)

But what’s happening on the web server returning HTML? How is the web server getting the HTML that it returns to our browser? And, to be even more specific, how does this happen in PHP?

This isn’t something that most of us know how to answer. We know (or at least should know!) how to build a PHP website. But what happens between our PHP code and the browser is a bit of a mystery.

There’s a good chance that you’re thinking, “Well, my site loads. Why should I think twice about this?” And that’s true! But the truth is that knowing how a PHP application works can be useful.

It can affect how you design your PHP application. But it’s also important if you’re looking to improve its performance. For example, it’s hard to understand PHP application caching if you don’t understand how a PHP application works.

Continue reading How does a PHP application work?

Mastering the use of PHP conditionals

No set of control structures is more pervasive in programming than if, elseif and else. With a few exceptions, you’ll use at least one per function or method that you write. There’s just no way around it.

But conditionals (that’s what we call these control structures) fit in the “easy to learn, hard to master” category. In fact, they’re so easy to use that you can develop some bad habits around them. (This is also a problem with loops.) This can lead to code that’s complex and hard to read or even test.

That said, it’s possible to develop good programming habits with conditionals. This is what this article will try to help you with. We’ll go over some programming techniques that can help make conditionals more manageable.

Continue reading Mastering the use of PHP conditionals

PHP strings and how to format them

Strings have always been an integral part of the programming world. We use them so much because they do something that’s almost impossible to do with other data types. They allow our code to communicate with others.

The definition of “others” is quite wide in this context. Others can be as straightforward as someone using your WordPress plugin. But it can also be more abstract like a browser reading HTML code generated by PHP. They are both a third party who interacts with strings generated by your code.

Sometimes, these strings are simple and used as is. But more often than not, these strings need to be dynamic. They need to adapt to what’s going on inside your code.

This latter one is why it’s important to have a good understanding of what you can do with strings in PHP. That’s because there are countless ways that you might want to alter a string. For example, you could have a string change based on certain conditions. Or you could insert one or more variables into it.

For this article, we’re going to focus on how you can format a string. There are a lot of built-in helper functions for strings in PHP. We’ll look at the ones that can help you achieve that goal.

Continue reading PHP strings and how to format them

Using inheritance with WordPress

As a WordPress developer, you’re always looking for ways to better reuse your code between projects. Your time is valuable and you don’t want to reinvent the wheel each time you start a new project.

Object-oriented programming can help you with that. In a previous post, I covered why you should learn it. Now it’s time to take things further by going over the main feature for code reuse. You’ve probably heard about it before. It’s called “inheritance“.

Continue reading Using inheritance with WordPress

The first thing you should learn from object-oriented programming

You’ve decided to learn object-oriented programming, but you don’t know where to start. Object-oriented programming has so many concepts and features. The whole thing can feel overwhelming at times. Let’s help you get started on the right foot.

What’s a great place to start? With the feature that you’ll associate the most with object-oriented programming. It’s called encapsulation. Most modern programming languages support encapsulation using classes.

Continue reading The first thing you should learn from object-oriented programming

Why object-oriented programming is your next step as a WordPress developer

You’re a WordPress developer. You might have a few plugins under your belt or a theme or two. You build WordPress sites for clients or just for yourself. You’ve heard of object-oriented programming, but, each time you look into it, it makes no sense!

You tell yourself object-oriented programming isn’t useful or worth the trouble. The goal of this article is to focus on explaining this value to you. At the end of this article, you should have a clear understanding of why you should learn it.

Continue reading Why object-oriented programming is your next step as a WordPress developer

Single responsibility principle, WordPress and You

For WordPress developers, it can be hard to improve your PHP skills. You lack the resources or tutorials to drive home these concepts. This happened just a few weeks ago when Nathaniel asked for help with his Stack Overflow question.

He was looking for help applying the single responsibility principle with WordPress. I sent him a bunch of replies but closed off saying I needed to write a post about it. This article is a detailed explanation of my thoughts following that conversation.

As WordPress developer, you might have started working with or looking into object-oriented programming. Maybe you even heard of something called SOLID.

As a whole, SOLID can be an intimidating topic. But the single responsibility principle is just one part of it (it’s the ‘S’ in SOLID). That makes it a more manageable topic to discuss and help you with.

At the end of this article, you should have a better understanding of it. You can also use the provided code samples in your own plugins and themes.

Continue reading Single responsibility principle, WordPress and You

Making you proud

So yesterday I wrote about my first day doing Justin Jackson’s growing an email list to 1,000 class and the struggle of first time bootstrappers to build an audience.

The class continues

Today, we have to evaluate who are target audience should be. I think it’s pretty common for most bloggers to start writing about anything. I started talking about health and business topics because that’s what I was working on at the time. The content was also mostly educational. It didn’t help the reader in any way unless you just wanted to learn about health behaviour! By the end of the first iteration of Helthe, it was obvious I didn’t feel like writing about it. You should be passionate about what you write about!

A lot of the advice given is similar to what I had discussed when I started the PHP App Challenge. You should focus on your competitive advantage and the groups you are already part of that have money to spend (important!). I am very involved with WordPress, but I see myself as a PHP programmer more than just a WordPress programmer. So I want to write to programmers, but specifically those that work with PHP.

Another fortuitous discussion

For our homework, we had to come up with a statement to describe our audience and why were choosing them. I was struggling with the why, but then me and Justin started a discussion in the JFDI chatroom where we discussed PHP as a whole. This led to some great insight.

It started with me asking whether I could target programmers and still talk about products. He commented how there isn’t anyone talking about products using PHP or building products with PHP. I actually learned that Mailchimp is built entirely with PHP. This moved on, as it always does, to discuss how PHP is the second-rate citizen of the web. This has been mentioned many times on this blog already and it’s even the key reason I started Helthe.

To make PHP devs proud to be PHP devs

That line came from Justin, but it’s a great cause that any PHP dev can relate too. And just like that, I had my why. It’s a great why too. I can’t think of a week where I didn’t see this come up in conversation either in person or on the web.

I think it is partially why the community strives to do so much. There are so many cool projects going on and great developers working on them. I would say in the past 3 years, there has never been a day where I am not proud to be a PHP dev.

So following this new yet not so new insight, I finalized my homework statement for day 2.

“My name is Carl Alexander. The audience I am choosing is programmers who are using PHP, because PHP is awesome and we should be proud of using it.”

So things are shaping up

There’s a bit more of a mission statement around what this mailing list is going to be about. If you’ve liked what I have been writing, you can subscribe to my mailing list here! You can also keep checking the blog every day if that’s your thing!

Why it is only PHP

Looking for an update on what I did yesterday? Well I did NOTHING. I felt quite bad about it, but if there’s something my gym habit has taught me is that you can’t be perfect every day. But today I was ready to get things done!

It is worth noting that it is the last day to work on your roadmap for Week of Hustle. Tomorrow, you have to ship things out so I was determined to get through the rest of it.

A week ago by the campfire

Last sunday following the publication of my roadmap, I had a discussion in the JFDI campfire chat room with Andy Parkinson about Helthe and what the service was. The important part of the conversation is found below.

Campfire ChatThe main takeaway that I took is that I needed to really double down on what it meant to the user to have a solution for PHP. The other popular languages at the moment tend to have one dominant framework (Django for Python and Rails for Ruby), but that’s not the case for PHP and that needs to be better explained.

PHP is 80% of the web

PHP is so prevalent for many reasons, but the important point is that PHP is represented by more than just a framework. It is used to run web dominant CMSes and E-Commerce platforms as well as various frameworks so it’s not realistic to expect one PHP library to be adequate for each. I definitely don’t believe it is. And that’s where I draw my line.

WordPress is the best example of that. I already did an exhaustive analysis of WordPress errors and how they are handled. A regular PHP library would only capture a fraction of the problems that could happen within a WordPress installation. It’s especially complex because WordPress is so defensive in its way of handling problems.

I don’t have an issue with that per say because it makes WordPress very user-friendly which is one of its core values. It does, however, put an increased burden on the programmer who is looking for the cause of problems. This can lead to dangerous practices like cowboy coding because it’s just so hard to know what is going on at times.

We meet again copywriting

With all these ideas swirling in my head, I set to work modifying my marketing site copy.

Now I have a confession to make, whenever I have to do copywriting, I want to go hide somewhere and forget about it. It’s one of the many skills I need to build, but it’s definitely the one that makes me feel the clumsiest. I feel like Fry (Futurama) trying to play the holophor for Leela.

You have well constructed ideas in your head, but you have to put them out there and hope they get shared properly.

Before
Before Screenshot

I wanted to modify the section that discussed the more PHP centric benefits for the user to be more descriptive and explain some of the  issues I brought up earlier in the post. After 4 hours, I got a result I was satisfied with.

After Screenshot
After Screenshot

I do actually think it is an improvement over the old version. I didn’t add the logos because I couldn’t make them look good on the page with the responsive layout so I kept them out. I am curious to know if logos are something you would consider important so let me know in the comments.

I did a small modification to the FAQ to say that the deadline had passed, but I was still working on it.

This goes up tomorrow

I’ll be pushing all this out tomorrow, but will be keeping the work I did on the account creation and API disabled. I’ll be writing up my thoughts on the experience as well.

Keep up with my progress

I have been bad and haven’t sent emails everyday as I should, this is the next thing I want to spend time tackling. I’ll be sending a detailed recap at the end of the project to my mailing list. You can subscribe here. You will also get all the info when things are ready for testing.