Simply Cutting Edge
Welcome Guest [Log In] [Register]
Welcome to zetaNetwork. We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as viewing zetaNetwork exclusive tutorials and articles, and access to our code snippets section. Registration is simple, fast, and completely free.


Click Here To Register


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
OOP For Noobs - Part 2
Topic Started: Jun 4 2011, 11:36 AM (9,782 Views)
Pro
Member Avatar
Underground Coding

OOP For Noobs - Part 2
Difficulty: Medium
Time: 20 Minutes



Previously...
OOP For Noobs - Part 1



Welcome back to OOP For Noobs. The tutorial series where you'll learn to utilize Object Oriented Programming in PHP. To start off we'll review what you should've learned in part one. You should've learned how to create a basic class, with various attributes and methods with different visibility modifiers. You also should've learned how to extend a class and how basic inheritance works. Let's start off with a quick exercise to get your brain moving. What's a method, well a method is basically a function that is part of a class. Sorry I forgot to mention that in part one.

Create a generic forum class that has the attributes name and type (free or paid). Your forum class should also include a get_name method and a get_type method that returns their respective values. Also include a get_info method that prints out the name of the forum along with the type. Now create 2 subclasses from the forum class, ZetaBoards, and IPB. Both of these subclasses should override the get_info methdo with their name, type, and where you can get these forum systems (URL). When you're done with this click the spoiler below to see how this program is done, or rather how I did it.

Exercise Solution

Now that we've warmed up we're gonna get to the tutorial itself. In this tutorial you'll be learning about interfaces and abstract classes. We'll start with interfaces.



What is an interface? An interface is similar to a class. The only exception is that it can not contain any attributes, method bodies, and it can not be instantiated. So what's the point of an interface? An interface is basically used like a template for your class. An interface would look something like this:
Code: PHP
 
<?php
interface myInterface{
public function mehtod_one();
private function method_two();
}
?>
Notice how instead of class we used interface on the first line. If you look at our class body we only have methods. Not just any methods, however, these methods don't have a method body. That is because since this is an interface, this is only a class template. In order to use an interface we have to implement it into a class.
Code: PHP
 
class myClass implements myInterface{
public function method_one(){
//some code
}

private function method_two(){
//some more code
}

public function method_three(){
//some more code
}
}
Notice how in the first line we implement myInterface via the keyword implements. This means the class myClass is implemented the interface myInterface. You may implement as many interfaces in a class as you want. How is an interface a template though? You may be thinking "I can just write in those methods myself." well an interface is a template because once you implement an interface all the methods in that interface must be implemented. Also notice how all the visibility modifiers match up with those in the interface. This is because an interface is a template and as such any methods that are defined in an interface must be implemented as defined in the interface. Although interfaces are used as templates you can also define additional methods for a class that were not defined inside any implemented interfaces. Notice the method method_three. Now let's move onto abstract classes.



What is an abstract class? An abstract class is basically something in between an interface an a class. An abstract class can contain class attributes and methods with method bodies, as well as methods without method bodies. To declare an abstract class just use the keyword abstract before the class keyword.
Code: PHP
 
abstract class myAbstract{
private $attribute;

public function method_one($someArgument){
$this->attribute = $someArgument;
echo $this->attribute;
}
}
Not much to abstract classes now that you know about interfaces. There are several differences however. For instance, an abstract class is extended instead of implemented. Wait, extended? Yes, this means that any methods that were defined in the abstract class are usable by the child class. As well as any attributes.
Code: PHP
 
class mySecondClass extends myAbstract{
function __construct(){
parent::method_one('Example!');
}
}
Now we're going to go over abstract methods. Abstract methods are why abstract classes are similar to templates like interfaces. An abstract method does not have a method body. Abstract methods also have the keyword abstract before the visibility modifier.
Code: PHP
 
abstract class myAbstract{
private $attribute;

public function method_one($someArgument){
$this->attribute = $someArgument;
echo $this->attribute;
}

abstract public function method_two();
}

class mySecondClass extends myAbstract{
function __construct(){
parent::method_one('Example!');
}

public function method_two(){
//Do Something
}
}


Simple enough right? I know I kinda went through this quickly but that's because the syntax and concept is relatively easy once you get used to OOP. I'll probably expand this tutorial series by adding a section about polymorphism right after this, as part three. If you have any questions feel free to post them here. Hope you learned something new :D
Pro | zetaNetwork Instructor & Admin
I Coded Most Of The Smexy Stuff You See :P
PM Me Any Questions
Need live support? Click here.
Offline Profile Quote ^
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Tutorials · Next Topic »
Add Reply