Publishing System Settings Logout Login Register
Introduction to Object-Oriented PHP
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on November 6th, 2008
3808 views
PHP Coding
http://www.bbarash.co.uk  - Homepage

Hiya - welcome to my introduction of Object-Oriented PHP or OOPHP for short.

The main focus of object-oriented programming is to make reusing code a lot easier. Classes are used to group functions and variables together as an object.

Functions are still used, but in object-oriented programming they are referred to as methods. Variables can still be defined within the methods but they can be defined as part of the class itself.

New objects that are created from classes are called instances.

Now for some object-oriented PHP examples.

I'll create a new class called Dog with three methods: eat, bark and lick. Class names follow the same naming rules as variables and functions so be careful how you name your classes. The code that makes up the class is placed between curly braces ({}).

<?php
class Dog {

// class code here

}
?>


The code above shows the basic class structure. The word class is written and then the class' name. Finally the class' content is placed between curly braces as mentioned before.

The next step is to create an instance of that class. The 'new' keyword tells PHP to output a new instance of the Dog class. Even though the class isn't currently doing anything it is possible to see that it has been defined as an object. The class can be thought of as a blueprint for building instances.

<?php
class Dog {

// class code here

}

$lottie = new Dog();
echo "Lottie is a new ".gettype($lottie).";
?>


The above example should output "Lottie is a new object". It shows the usage of 'new' keyword in creating a new instance.

Next is defining a method (function) within a class. They operate within the environment of the class as well as its variables. There is a special method called a 'constructor' that is called when a new instance of a class is created to do any work that starts the class e.g setting up the values of variables in the class. The constructor is written by creating a method that has the same name as the class:

<?php
class Dog {

 // Constructor below
  function Dog() {
  }

}
?>


Lastly, classes can contain user defined methods. For the Dog class you can define eat, bark and lick.

<?php
class Dog {

 // Constructor below
  function Dog() {
  }

function eat() {
 echo "Eat, gobble, gobble!";
}


function bark() {
 echo "Bark, woof, woof!";
}


function lick() {
 echo "Lick, slobber, slobber!";
}

}
?>


That's it for now, I'll write a more advanced OOPHP tutorial soon including function calling, inheritance and variable scopes!

Thanks for reading, and if it wasn't already obvious - yes I have a dog.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
BBarash

I am a freelance website design and web based programming enthusiast who resides in London. I spend my time learning and expanding my programming knowledge, as well as creating/designing sites for clients.

I'm adept in (x)HTML, CSS, PHP, MySQL, AJAX an
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top