Jump to content


Photo
- - - - -

[PHP] Introduction to Object-Oriented PHP


  • Please log in to reply
No replies to this topic

#1 BBarash

BBarash

    Young Padawan

  • Members
  • Pip
  • 1 posts
  • Gender:Male
  • Location:London, UK
  • Interests:Computing, Programming, Designing, Web-Mastering, Astronomy, Chemistry, Digg, Twitter and Diggnation.<br /><br />Being a nerd!

Posted 22 October 2008 - 10:31 AM

(Originally posted and written for www.nerdwired.net)

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 ({}).

[codebox]<?php
class Dog {

// class code here

}
?>[/codebox]

The code above shows the basic class structure. The word class is written and then the class's name. Finally the class's 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.

[codebox]<?php
class Dog {

// class code here

}

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

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 methodthat has the same name as the class:

[codebox]<?php
class Dog {

// Constructor below
function Dog() {
}

}
?>[/codebox]

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

[codebox]<?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!";
}

}
?>[/codebox]

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.

Edited by BBarash, 23 October 2008 - 01:57 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users