Web Design Forum: OOP PHP Part 1: Your first Class - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
-----
Before we can delve into the more complex aspects of OOP, we need to get you started with building a class. A class is a blueprint, from which you produce instances (commonly referred to as “instantiation”).

Classes contain a combination of properties and methods. These are variables and functions (respectively) that exist within the class scope. More on “scope” in a minute – but first – let’s make a class with a property


class MyClass(){

    public $property;

}



So, to use this powerhouse of code, I would first “instantiate” the class:


$MyClass = new MyClass; //produces a copy of the class and stores it in $MyClass

$MyClass->property = ‘Hello!’; //adds a value to the property.



You’ll notice the “->” sign between the variable containing the class, and the method of the class we want to call. That’s called an “object operator”, and it points to something within the object itself. So, when we want to work with something inside the object – we use the operator to point to it. Let’s do something with the property:


echo $MyClass->property; //prints ‘Hello!’;



The word “public” in the class means that we can access the property from outside the class using the operator. Sometimes you might want to keep aspects of your class private – and funnily enough, that’s the word we use! Let’s change the property’s visibility to private.


class MyClass(){

    private $property = ‘sunshine’;

}



Now if you try to access it externally:


echo ‘Hello ’ . $MyClass->property; //throws an error

$MyClass->property = 'New property';//also throws an error



This is obviously really handy - it prevents anyone from reading or changing important properties of your objects.There’s a third visibility type called “protected” – but we’ll worry about that later.

Next, let’s define a method.


class MyClass(){

    private $property=’sunshine’;

    public function hello($var){

    echo ‘Hello  ’.$var.’!’;

    }

}

$MyClass->hello(‘Sunwukung’);//prints ‘Hello Sunwukung'




Methods in a class work just like regular functions, you can pass them arguments. Class methods have a few more tricks up their sleeve though. Enter $this...let’s change the method slightly:


public function hello(){

echo ‘Hello ’.$this->property.’!’;

}

$MyClass->hello(); //prints ‘Hello sunshine’;




While you can’t access the private property from outside the class, you CAN access the method. Since the method is inside the class – it also has access the private property (or any other class components) by using the word $this. So you can access the private property of the class via the public method which has access to it...this kind of method is normally called a getter and would normally be called something like getProperty(). You can also use the same system for setting private properties within a class, and as you might expect, this is called a setter.


Finally – let’s talk about constructors. Whenever you “instantiate” an object, it is “constructed”. In fact, you can define a method that always gets executed whenever you create an object using the new word specifically. This is an important point which I'll come back to in the next post.



class MyClass{

private $property

public function __construct($property){

    $this->property = $property;

}

public function getProperty(){

    return $this->property;

   }

}


$class = new MyClass('sunshine');//the constructor sets the private property to 'sunshine'

echo 'Hello' . $class->getProperty();//prints sunshine



That about wraps up this post - things are going to pick up pretty fast from here on in, so play around with these ideas until the next time. In the next post, I'm going to show you a practical, real world application of OOP and design patterns.
0
 

1 Comments On This Entry

Imho, names are too abstract :/ Should have better use $earth = new Planet, or similar.
0
Page 1 of 1

May 2012

S M T W T F S
  12345
6789101112
13141516 17 1819
20212223242526
2728293031  

Recent Entries

My Picture

Recent Comments

Categories