Object Oriented PHP

John Wolfe
2 min readMay 14, 2018

This week I spent some time reorienting myself to some of the fundamental concepts involved in object-oriented PHP.

Although I’ve only scratched the surface of this concept in PHP, in many ways it reminds me of creating classes in JavaScript more so than it does the creation of classes in Ruby. Similar to JavaScript, PHP’s classes are instantiated similar to JavaScript, they utilize a “this” keyword, and there is also a constructor function that is used at times. (Although these similarities are not entirely parallel).

In PHP, classes are established through the familiar class keyword. For instance, creating a Dog class would look like the following:

class Dog {}

Adding a function to the Dog class is simple:

class Dog {public function bark() {return “Bark bark!”}}

And instantiating an instance of this dog class would occur through the familiar method:

$rover = new Rover

And calling the bark function would be similarly easy:

echo $rover->bark()=> “Bark bark!”

In PHP, it’s also possible to provide a class specific properties. Accomplishing this requires a constructor function. For instance, following the example above, it’d be possible to add properties to the Dog class like so:

--

--

John Wolfe

Software developer Tata Consultancy Services. React, Rails, and Java. Former content editor for @Quora and @inversedotcom. I live in Chicago, Illinois.