TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
View Poll Results: Was this useful?
Yes 8 100.00%
No 0 0%
Voters: 8. You may not vote on this poll

 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 03-28-2008, 02:26 AM   #1 (permalink)
The Contributor
 
quantumkangaroo's Avatar
 
Join Date: Feb 2008
Location: Pretoria, South Africa
Posts: 42
Thanks: 1
quantumkangaroo is an unknown quantity at this point
Smile PHP5 Classes A to Z Part 1

Due to some wanting a more advanced tutorial on classes and functions I'm back and ready to please, so lets get cracking shall we. For this tutorial we will be leaning more towards PHP5 so feel free to comment if something does not work for you. We will be doing an example of a mail class. Walking through it step-by-step

The improved object model in PHP5 makes developing applications using OOP much easier and gives you, the programmer greater flexibility.

What is a class?

A class is a collection of methods and objects.

What is the purpose of a class?

It's the same reason as any other programming language: for large projects, classes provide superior organization and less repetitive code.

What is the difference between classes in PHP 4 & PHP 5?

A few notable differences between the two versions are;
  • Visibility keywords were added (public, private, protected)
  • The class constructor is now __construct() instead of the Class_name()
  • Static keyword
  • Abstract Classes
  • Interfaces
  • Throwing Exceptions
  • Type Hinting and Delegating

The class structure

The class structure is pretty simple and straight forward;

PHP Code:
<?php

class TalkPHPMail {

 var 
$to null;
 var 
$from null;
 var 
$subject null
 var 
$headers null;
 var 
$body null;
 
 function 
newmail($to,$from,$subject,$body) {
  
// Everything you want to do in the function
   
}

}

?>
First we specify and declare our class, we open our brackets and declare all variables to be used in the class. Note that all variables that are to be used have to be declared before any methods that make use of them. After this we can start with our functions supplying whatever we need to.

Visibility of your data

As from PHP5 visibility keywords have been added to the OOP model to make accessibility management easier for us. The visibility of a property or method can be defined by prefixing the declaration with the the visibility keywords;
  • public - The variable can be accessed and changed globally by anything.
  • private - The variable can be accessed and changed only from within the class.
  • protected - The variable can be accessed and changed only by direct descendants (those who inherit it using the extends statement) of the class and class its self

As a side note although the keywords have been brought into the our PHP lives, this has not affected the var prefix, it is still acceptable to use it. It is equivalent to the public keyword.

Lets see how our class would look with some visibility keywords;

PHP Code:
<?php

class TalkPHPMail {

 public 
$to null;
 public 
$from null;
 public 
$subject null
 public 
$headers null;
 public 
$body null;
 private 
$info null;
 
 function 
newmail($to,$from,$subject,$body) {
  
// Everything you want to do in the function
   
}

}

?>
We have denoted all the variables that will need to be supplied values outside the class with the public keyword, and the variable which we don't want to be changed outside of the class to private. Attempting to violate these rules in your program will generate a fatal error. Keywords may also be appended to methods.

Constructing and Destructing

PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

Destructor is a function which is called right after you release an object. Releasing object means that you do not need it or use it anymore. This makes destructor suitable for any final actions you want to perform.

PHP Code:
<?php

class TalkPHPMail {

 public 
$to null;
 public 
$from null;
 public 
$subject null
 public 
$headers null;
 public 
$body null;
 private 
$info null;
 
 function 
__construct() {
  
// New object is created
   
}

 function 
__destruct() {
  
// Object is released
   
}

}

?>
Yay for Abstract Classes

In the simplest sense, an abstract class is a class that cannot (and should not) be instantiated. This may sound a little illogical for beginning programmers, but there are situations where this concept makes a lot of sense. Abstract classes are very handy for large Web applications, where a well-structured hierarchy of classes is often required.

Certainly, this doesn’t mean that you have to define abstract classes here and there, whenever you can possibly use them. Most of the time, implementing abstract classes requires careful planning and study of the relationships between the classes that make up an application.

Abstract classes should only be used to be inherited or extended, for example;

PHP Code:
<?php

abstract class Subject {
  private 
$subject;
 
  protected function 
__construct($subject) {
     
$this->subject $subject;
    }
}

class 
TalkPHPMail extends Subject {
   private 
$message;
   public function 
__construct($message,$subject) {
     
parent::__construct($subject);
     
$this->message $message;
    }
}

?>
Implementing Interfaces

An interface is similar to an abstract class, indeed the occupy the same namespace as classes and abstract classes, (hence you cannot define an interface with the same name as a class). An interface however, is a fully abstract class, none of its methods are implemented and, instead of a class subclassing from it, it is said to implement that interface.

Each method declared in the interface must be defined by any class which implements it having at least the parameters identified. It may have more parameters (as long as they are optional), but it cannot have less.

PHP Code:
<?php

interface MailFunc {
    public function 
send();
    public function 
receive();
}

class 
TalkPHPMail implements MailFunc {
    public 
$mail;
     
    public function 
send($to=''$from=''$subject=''$body='') {
        
$this->mail mail($to,$from,$subject,$body);
      }
    public function 
receive($from='') {
        
// Your receive function
      
}

?>
Type Hinting

Type Hinting is a new facility in PHP 5 which enables you to force function arguments to be objects of specific types. Before PHP 5, the only way of ensure that function arguments were of a specific object type was to use the type checking functions provided. Now we can simply enforce the object type by proceeding the function argument with its name.

PHP Code:
<?php

class Subject {
  public 
$subject 'Hello TalkPHP!';
}

class 
TalkPHPMail
{

  public function 
__construct(Subject $newsubject) {
    echo 
$newsubject->subject;
   }
}

?>
Static and Final

Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

PHP Code:
<?php

final class TalkPHPMail {
   public function 
send() {
       echo 
"Sending....\n";
   }
}

class 
Subject extends TalkPHPMail {
 
// This will result in a fatal error
}

?>
The Scope Resolution Operator

The Scope Resolution Operator or double colon :: allows us access to methods of a class. It is used in object-oriented programming when you want to be specific about what kind of function you are calling. The most common use for scope resolution is with the pseudo-class parent.

Two keywords, self and parent are used to access members or methods inside a class.

Outside the class;

PHP Code:
<?php

class TalkPHPMail {
   const 
subject 'Hello TalkPHP!';
}

echo 
TalkPHPMail::subject;

?>
Inside the class;

PHP Code:
<?php

class TalkPHPMail {
   const 
subject 'Hello TalkPHP!';
   public function 
send() {
       echo 
self::subject;
   }
}

?>
I think my brain is a little fried after that, we'll continue with Patterns and Exceptions in part 2 of the guide. Please feel free to comment or correct me as I doubt its 100% problem free it's 4 AM here :D
__________________
virtueCart v1.0.5 developed by WebDevSA


Last edited by quantumkangaroo : 03-28-2008 at 02:28 PM.
Send a message via MSN to quantumkangaroo Send a message via Skype™ to quantumkangaroo
quantumkangaroo is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:41 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design