Web Design and Programming Pt 10 OOP & PHP

Object Oriented ProgrammingIn this PHP video tutorial I cover Object Oriented Programming with PHP. I specifically go over the following topics:

  • Encapsulation
  • Classes
  • Objects
  • Public
  • Private
  • Constructors
  • Destructors
  • __set
  • __get
  • And more….

All of the code from the tutorial is available after the video. Use it in anyway that you like. I’ll continue covering OOP with PHP in the next video tutorial in this series.

If you have any questions leave them below.

All the Code from the Video

<title><?php echo “Object Oriented Programming”;?></title>


# Encapsulation lets you hide the code behind an interface so you can protect the code
# and so your user need not understand the code to use it

# A class is a blueprint you use to create objects

# Objects contain the data (attributes) and actions (methods) needed to perform its tasks

# Inheritance is done when you create a new class from another, thus borrowing the data
# and methods that can be found there.

# Polymorphism allows a class to perform differently based on how it is being used.
# Polymorphism can be used to a certain extent with PHP in that you can use common
# method and attribute names for similar classes

# Any use of this is a reference to a specific object and that objects attributes and methods

class Animal
public $name; # Public variables can be accessed directly by anyone
private $age; # Private variables are only accessed by class methods, but not subclasses
protected $money; # Protected is like private except can be accessed by inherited classes
const POUNDID = 12345; # A constants value can never change
# A static attributes value is shared by all objects of the class
# You refer to static attribute like this self::$numOfAnimals, not with $this
private static $numOfAnimals = 0;

public function __construct($age)
echo “__construct was called”. “<br />”;

public function __destruct()
echo “__destruct was called”. “<br />”;

# __set can be used to perform error checking before assigning values to private attribs
function __set($attribName, $attribValue)
echo “__set was called for “. $attribName. “<br />”;
$this->$attribName = $attribValue;

# __get can be used to perform error checking before returning values from private attribs
function __get($attribName)
echo “__get was called for “. $attribName. “<br />”;
return $this->$attribName;

public function getAge()
return $this->age;

public function setAge($sentAge)
$this->age = $sentAge;

$dog = new Animal(8); # Create an object dog of class Animal
$dog->name = “Grover”; # Assign a value to the public attribute name
$dogName = $dog->name; # Retreive value from public attribute name

$dog->setAge(8); # Call the setAge() method for the dog object

$dog->age = 9; # Try to set a private attribute that makes a call to __set
echo $dog->age. “<br />”; # Try to get a private attribute that makes a call to __get

echo “The dogs name is ” . $dogName . “<br />”;
echo “The dogs age is ” . $dog->getAge() . “<br />”;
echo “The pounds ID number is “. $dog::POUNDID. “<br />”;



8 Responses to “Web Design and Programming Pt 10 OOP & PHP”

  1. akasuna says:

    Good tutorial!

    But I think it should be called “property” instead of “attribute”, how do you think about it?

  2. Bruce says:

    When do you suggest we use OOP vs Proc? and, can you give me an example of polymorphism in php?

    • admin says:

      I love PHP, but it really isn’t an oop language. It tries to fake it, but I write procedural php code most all of the time. Is it possible to use design patterns in php that require an object capable language yes. I’ll cover this in more detail when I cover design patterns.

      Long story short I don’t believe oop is needed unless you find yourself using tons of conditional statements. I hope that helps

  3. Greg says:

    Great Tutorial. I’m really liking your detailed, methodical approach. Just what I need to get it through my thick skull.

    What tool are you using that allows you to see the php working as you write it? I use Dreamweaver for HTML and coding, and I have to upload to a server to see it run.

    Also, For some reason, Dreamweaver doesn’t like this syntax:
    echo “The pounds ID number is “. $dog::POUNDID . “”;
    for some reason. But it runs in the browser.

    • admin says:

      Thank you 🙂 I’m glad you enjoy them so much. I have a basic text editor on the left and Google chrome on the right. I can execute the code because I’m running everything on my localhost computer. The name of the text editor is Text Wrangler (Mac), but if you’re on a PC Notepad++ works great. Both are free! Thanks for taking the time to say hi

Leave a Reply

Your email address will not be published.