Tag Archives: Object Oriented Programming

Web Design and Programming Pt 11

Object Oriented ProgrammingHere I continue to explain Object Oriented Programming with PHP. If you missed the first part definitely watch that video tutorial first Object Oriented Programming with PHP.

Here I’ll cover the following topics:

  • Inheritance
  • Static Attributes (Variables)
  • The _destruct Function
  • The Final Keyword
  • Overriding Functions
  • And more…

If you have any questions, leave them below. All of the code follows the video.

All the Code from the Video

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

<body>

<?php

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

class Animal
{
private $name; # Private variables are only accessed by class methods, but not subclasses
private $favFood = “meat”;

# A static attributes value is shared by all objects of the class
# You refer to static attribute like this self::$numOfAnimals, not with $this
public static $numOfAnimals = 0;

# Constructor and Deconstructor
public function __construct($name=”No Name”)
{
echo “__construct was called”. “<br />”;
$this->setName($name);
self::$numOfAnimals++;
}

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

# Get and Set Methods – final is used to keep methods from being overwritten
final public function getName()
{
return $this->name;
}

final public function setName($sentName)
{
$this->age = $sentName;
}

# Animal Default Functions
public function makeNoise()
{
echo “Grrrr”. “<br />”;
}
public function favFood()
{
echo “My favorite food is ” . $this->favFood . “<br />”;
}
public function move()
{
echo “Walk around”. “<br />”;
}

}

class Dog extends Animal
{
# If I didn’t create a new constructor the parent would be called
public function __construct($name=”No Name”)
{
parent::__construct($name); # How to call the parent constructor
# Animal::__construct($name); this would also call the parent constructor
}

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

# Dog Default Functions Overwrites Class Function
public function makeNoise()
{
Animal::makeNoise(); # How to call the original class method
echo “Bark, Bark”. “<br />”;
}
}

#function animalStuff(

$grover = new Dog(“Grover”); # Create an object grover of class Dog
$paul = new Dog(“Paul”);
$grover->makeNoise(); # Call the makeNoise() method for the grover object
$grover->favFood();
$grover->move();

echo Animal::$numOfAnimals. “<br />”; # Prints out the number of objects created

?>

</body>
</html>

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

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

<body>

<?php
# 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 />”;
$this->setAge($age);
}

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 />”;

?>

</body>
</html>

Python 2.7 Tutorial Pt 7

PythonIn this video, I will explain the basics of Object Oriented Programming with Python. Understand that a Class is the blue print that defines what Attributes (variables) and Methods (functions), each object it builds must have.

Each Object that is created is said to be an Instance of the Class that built it. I’ll also go over what Encapsulation is.  Encapsulation is how you hide information  and constrain a user of your class to use it in the way you define. And, finally I teach you how to enforce Encapsulation by making your Attributes and Methods private.

Like always, a lot of code follows the video. If you have any questions or comments leave them below. And, if you missed my other Python Tutorials they are available here:

Continue reading Python 2.7 Tutorial Pt 7

iPhone Development: Objective C Pt 5

Object Oriented ProgrammingObject Oriented Programming with Objective C

In a previous article, I explained how you create object’s, instance variable’s and method’s in Objective C. In this article, I’ll explain Inheritance, Polymorphism and go over other OOP topic’s I missed previously.

Briefly, Inheritance provide’s you with a way to build additional capability’s onto existing class’s.

Polymorphism is a little more complicated. It allows you to call a parent object method and it will sort out which class object method should be executed.

Continue reading iPhone Development: Objective C Pt 5