Close

oop

Object-oriented programming in PHP

This just can get confusing as hell, sometimes.

I kinda just shake my head at the “Intro to OOP” tutorials that spend the whole time talking about “polymorphism” and “encapsulation”. It’s no wonder a lot of developers hold out learning OOP.

Anyway, the most important…

Most fundamental…

Object-oriented programming principle is much, much simpler.

And, I think a big “aha” for developers.

Of course, that’s probably just my naive “don’t confused the hell out of people on day 1” opinion, but who knows. Anyway, if you’ve been wanting to tackle OOP in PHP, but been afraid it’s complicated as hell…

Give this video a watch:

I think you’ll see… it’s a lot simpler than you think.

(And, most coding instructors make it out to be.)

If you do, I’d appreciate it if you’d share it with anyone you know who is also looking to learn object-oriented programming.

Later,

John

November 5, 2018

Static Methods and Properties In PHP

In my last tutorial, What Object-Oriented Programming Is NOT, I mentioned that over-using or mis-using the static keyword is one way developers get confused with object-oriented programming. That, often, they simply take their procedural code, dump into a class, make everything static and away we go.

That said, there CAN be legitimate uses for fully static classes.

I like the answer Pascal MARTIN gave on StackOverflow:

The distinction I make is this:

  • Use an instantiated class for actual objects in your applications (post, category, user, etc)
  • Use a static class for “libraries of code” that have useful functions

As Pascal said, that’s probably over-simplified, but it helps me keep my head straight. Of course, the question becomes… how do we write a static class.

The first thing to keep in mind is “once you go static, you can’t go back”… in a way. More specifically, you can’t use object properties or methods in static methods. So, this won’t work:

<?php
class Library {
public $var = ‘Hey’;

public static function do_stuff() {
echo $this->var;
}
}

Library::do_stuff(); // Triggers: Fatal error: Using $this when not in object context

That’s because $this references and instantiated object. But, we bypass that when using static methods. So, $this essentially doesn’t “exist”. But, you might think you could do this:

<?php
class Library {
public $var = ‘Hey’;

public static function do_stuff() {
echo Library::$var;
}
}

Library::do_stuff(); // Triggers: Fatal error: Access to undeclared static property: Library::$var

So, once you declare a method static… basically everything it touches has to be static, too. Thus, this is what works:

<?php
class Library {
public static $var = ‘Hey’;

public static function do_stuff() {
echo Library::$var;
}
}

Library::do_stuff(); // Outputs “Hey”

One way you might use this, then, is something like this:

<?php
class Library {
public static $date_format = ‘F jS, Y’;

public static function format_date($unix_timestamp) {
return date(self::$date_format, $unix_timestamp);
}
}

echo Library::format_date(time()); // Outputs formatted date

The format_date() method is one that could be used in various places throughout your application and wouldn’t need constantly re-instantiated to use. And, of course, this Library class could contain various, similar methods that have data-agnostic uses. A kind of “catch-all” for miscellaneous functions.

And, of course, as you’ve seen the way to make a property or method static is to simply add the “static” keyword to the declaration, like so:

<?php
class Library {
public static $var = ‘Hey’;

public static function do_stuff() {
// do stuff
}
}

So, there you go. Now, if you want to keep going and learn how to build professional PHP applications using object-oriented programming, then check out my full object-oriented programming course here (also available on Udemy here).

January 2, 2018

Inheritance In a PHP Class and Object-Oriented Programming

Let’s talk about inheritance in PHP. First, here’s how you do it:

<?php
class Daddy {
public function my_method() {
echo “I’m the parent”;
}
}

class Son extends Daddy {

}

$child = new Son;
$child->my_method(); // This outputs “I’m the parent” from the parent class

The child is the class DOING the extending. The parent is the class BEING extended. And, the child inherits any public or protected properties and methods from the parent. This is why “Son” has no methods, but still outputs the results of my_method(). Because it inherited that method from Daddy.

But, private methods and properties are NOT inherited. So, this won’t work:

<?php
class Daddy {
public function my_method() {
echo “I’m the parent”;
}

private function you_cant_haz() {
echo “Only Daddy gets me!”;
}
}

class Son extends Daddy {

}

$child = new Son;
$child->you_cant_haz(); // This triggers a fatal error

You can also override inherited methods in the child. Like this:

<?php
class Daddy {
public $var = “Call me Daddy! “;

public function my_method() {
echo $this->var;
}

private function you_cant_haz() {
echo “Only Daddy gets me!”;
}
}

class Son extends Daddy {
public $var = “Uh, that’s creepy. No!”;

public function my_method() {
echo $this->var;
}
}

$parent = new Daddy;
$parent->my_method(); // This outputs “Call me Daddy!”

$child = new Son;
$child->my_method(); // This outputs “Uh, that’s creepy. No!”

One example use case for this is WordPress widgets. When creating a new widget, your individual widget class extends WP_Widget to access and override its default methods. WordPress then does the “magic” of putting your widget together simply by you overriding the WP_Widget methods in your class.

Another example is the example OOP application shell I show you how to build in my object-oriented programming course. I give you a primer on MVC, which is obviously super popular right now, but is only one among many design patterns. But, I also show you another pattern I learned a few years back that I personally like better… and it involves some strategic inheritance like this. You can learn more about and enroll in my object-oriented programming course here.

December 21, 2017

[Periscope] How to Learn Object-Oriented Programming in PHP

In this week’s WebDev Q&A, I answer questions on:

  • There is so much to learn and it’s changing all the time, how do I know where to focus and what to choose to pursue?
  • How do I learn object-oriented programming?
  • How can one tell if I am good enough?
  • I’ve learned HTML and CSS, how do I make the transition into learn PHP, MySQL and Javascript?
  • I’m learning how to work with databases, what all do I need to learn?

Watch below:

Links mentioned in the video:

 

If you get value from this video, please consider sharing it with another developer or group who could benefit from it.

October 21, 2015