Nov
18
2008
0

OOP: Class Constants

It is recommended to read about Constants first before reading about Class Constants.

Class constants are the same as regular Constants except they are decliared within a class. Once they are decared they are accesable from all scopes of the script. Class constants simular to Constants can only contain scalar values.

Class Constants have some advantages over constants is that when declared within a class it is much cleaner code and are significantly faster than constants declared with defne().

PHP Class Constant “Hello World” Example:

class foo {
const BAR = "Hello World";
}
echo foo::BAR;

Constants in General:
Constants differ from normal variables in that you don’t use the $ symbol to declare or use them.
Must be a constant expression (not a variable, a class member, mathematical operation or a function call).

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming |
Nov
18
2008
2

OOP: Type Hinting

PHP 5 allows limited type hinting. This allows you to specify that the parameter to a function or class method can only be of a specific class (or one of its subclasses), or an array. However, you may not specify any other scalar types.

Put simply Type Hinting, is restricting the input of data by the datatype. An example would be a function that pints out an array data, we dont want to be passing this function null data, numeracy data or stings as it would cause errors. We do however want to pass it array data:

function test_array(array $input_array) {
print_r($input_array);
}

As you can see before we specify the data variable $input_array we specified the data type it should be array.

PHP Manual Type Hinting
Type Hinting Pro’s and Cons

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming |
Nov
17
2008
0

OOP: Reflection

Reflection is a small subject and will not be normaly used in your finished project, or shown to the end user. Its purpose is mainly for documentation purposes.

Reflection can examine the contents of a script’s code, such as functions and objects, at runtime. This means it outputs the code with definition of each aspect of the section of code.

Example Object, followed by reflection:

<?php
 
class Example {
    static function printer () {
        echo "Hello World!\n";
    }
}
 
$class = new ReflectionClass('Example');
$method = $class->getMethod('printer');
echo $class;
echo '<hr>';
echo $method;
?>

The output of this is:
Class [ class Example ] {
@@ D:\wamp\www\zce\reflection.php 3-7

- Constants [0] {
}

- Static properties [0] {
}

- Static methods [1] {
Method [ static public method printer ] {
@@ D:\wamp\www\zce\reflection.php 4 - 6
}
}

- Properties [0] {
}

- Methods [0] {
}
}


Method [ static public method printer ] {
@@ D:\wamp\www\zce\reflection.php 4 - 6
}

As you can see it outlines all the aspects such as constants and static properties. It shows you the class and information such as what lines the class is on (lines 3 to 7) it also does the same for the method (function) within the class (lines 4-6). Other information such as the directory’s and file that the class is in.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming |
Nov
17
2008
0

OOP: Autoload

Before PHP5 if you call a class that is not defined (not their) it caused an fatal error. This means you have to include all of the class files rather than loading them when required.

PHP5 solves this by introducing “autoload” this allows you to load classes on demand. If you try instantiation an object php will try the __autoload() function so that the script may load it, if autoload can not find te class it throws a fatal error.

Here is an example:

function __autoload($class){
// Require PEAR-compatible classes
require_once str_replace("_", "/", $class);
}
$obj = new Some_Class();

As you can see the script wants to load some_class() but that is not available! So it goes to autoload and tries to open some_class.php.

Although __autoload is useful it is advised to include classes manually this is to ensure autoload does not become bulky and slow.


PHP5 also introduces “Standard PHP Libary” which has its own lazy loading. SPL allows you to stack autoload on top of each other, which means if the first autoload can not find the class the next one may do so.

SPL autoloader is called spl_autoload(); It checks all include paths for filenames that match the class that needs loading it looks for .inc or .php files. You can add additional extensions by using spl_autoload_extensions();

As mentioned additional autoloaders can be added using spl_autoload_register(); BUT if this is used then the deafult __autoload is removed, so if you still require this remember to add it spl_autoload_register(’__autoload’); or use:

if (function_exists(’__autoload’)) {
spl_autoload_register(’__autoload’);
}
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming |
Nov
17
2008
1

OOP: Static Methods

This article is different because it deals with only the creation of a static method. Before we continue, we will insure we know the difference between a non-static class method and a static method.
Non-Static Method:
Static Method:

The real world example of the differences between a non static method and static method can be an old full sized computer tower. In the static method point of view, assume you only want to use the computer tower as a foot rest when you sit at your desk. For this, you do not need to turn on the computer (create a class object). This does not require you to load you operating system (no instantiating of class variables required). But, you can still kick up your feet. However, because you did not turn on your computer, you will be unable to access non-static methods (can only call on static method methods).

In the non-static method point of view, if you want the operation system, you would need to turn on the computer (create class object). What this does is load your operating system (instantiate class variables) so you can finally login to your computer (method of the class). Additionally, with the computer on, we can still kick our feet on the tower (can call both static and non static function).

Now how do we create a function with static methods in PHP? When we write the static method, we must use the keyword static. This method we create will always return true and is created in the common class.

class common {
   public static function myStaticFunction()
   {
      return true;
   }
}

Now that we have created the static function, we can call on the static method with the following snippet of code. The first term “common” is the name of the class. This is followed by two colons (::) followed by the method name.

$value = common::myStaticFunction();

And there we have it! We have successfully created and used our static method. To see the official PHP article on static, go to http://www.php.net/manual/en/language.oop5.static.php or http://www.victorchen.info/how-to-create-php-static-method/

Facts

Declaring class members or methods as static makes them accessible without needing an instantiation of the class
Static properties cannot be accessed through the object using the arrow operator ->.
the pseudo variable $this is not available inside the method declared as static.
Calling non-static methods the way you call static methods generates Warnings

Unlike regularmethods and properties, their static counterparts exist and are accessible as part of a class itself, as opposed to existing only within the scope of one of its instances. This allows you to treat classes as true containers of interrelated functions and data elements—which, in turn, is a very handy expedient to avoid naming conflicts.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming |
Nov
11
2008
0

OOP: Exceptions

Exceptions is a new OOP way of handing errors. They are used to change the flow of your code if a specified error arises.
(more…)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming |
Nov
07
2008
0

OOP: Interfaces

Interfaces in PHP allow you to define a common structure for your classes. An interface cannot be instantiated on its own.
Interfaces make reuseing code a much easier process. The interface methods have no internal logic, they are simply a “mapping” or constraint of what the class, or classes, should implement. Here we will demonstrate how this works using vehicle class, with the addition of a stop() function.

Here is an example in use:

<?php
interface fax{
  public function dial();
  public function send();
  public function recieve();
}
 
interface printer{
  public function printBlack();
  public function printColor();
  public function printDraft();
  public function kick();
}
 
class printerFax implements fax, printer{
  public function dial(){ }
  public function send(){ }
  public function recieve(){ }
  public function printBlack(){ }
  public function printColor(){ }
  public function printDraft(){ }
  public function kick(){ }
}
 
  $object = new printerFax;
?>

This code will output nothing, but that means its working. If we remove public function kick(); from printer then it will start complaining! .. This is because interfaces MAKE SURE that the aspects are used.

Read More:
http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html#10

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming |
Nov
07
2008
0

OOP: Visibility or PPP operators

PPP also known as Visability, sets where a method (function) or property (variable) is avaliable in the scope (scope is the section of the script. Such as class scope, method scope or globlal scope).

Their are four levels of Visibility:
public can be accessed from any scope.
protected can only be accessed from within the class
where it is defined and its descendants.
private can only be accessed from within the class
where it is defined.
final is accessible from any scope, but cannot be
overridden in descendant classes.
(more…)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming |
Oct
16
2008
3

OOP: Inheritance and Modifiers

Simply class’s can be parents, and have children we have gone trough this before. This means that if a method used in Class A is needed in Class B we can tell class b to extend from class a. This will give us all the data and methods from both classes that we want.
(more…)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming |
Oct
15
2008
1

OOP: Instantiation

Instantiation, is another complex word meaning creating an object. DYK: At first PHP wasn’t a OOP language!

A CLASS is a package containing two things: data and methods. Methods are just functions within a class, they obviously change name to make it more complex.The data portion consists of variables; they’re known as properties. The other part of a class is a set of functions that can alter a class’ properties; they’re called methods.

Remember that Objects can be parents, and allow their children to access parts of its contents.This is called inheritance and is one of the major advantages of classes over functions
(more…)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Adam in: 04. Object Oriented Programming | Tags:

WordPress Powered, Theme by TheBuckmaker.com | Add to Technorati Favorites. | RSS and Comments RSS