Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP OOP guide
#1
Good luck!

Welcome
Today I will show you what Object Oriented Programming (short: OOP) is, and how it works.
If you just started learning PHP, I recommend you reading my PHP Beginners guide or Tim's PHP 101.
For the case where you don't understand something, just ask in this thread.

What is OOP
http://wikipedia.org/wiki/Object-oriented_programming Wrote:Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs.
Programming techniques may include features such as information hiding, data abstraction, encapsulation, modularity, polymorphism, and inheritance.
It was not commonly used in mainstream software application development until the early 1990s.
Many modern programming languages now support OOP.

The Syntax
To define a Object (Class), you must begin with the keyword class, after the definition keyword you give your Class a name.
The Structure of the class is then written between a pair of curly braces.
As it is with functions and variables, the name of the class can be anything you wish, but not any of PHP's reserved words.
Letter or underscore are the only characters which are allowed as first character in the name.

PHP Code:
// class _tutClass : valid
// class 12Class   : fatal error
class tutClass {
    
// Object life


Properties
Every class can have it's own variables, constants, functions, those are called properties and are defined with the keywords,
var(DEPRECATED), public, private, protected, const.

http://www.php.net/ Wrote:In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private.
However, var is no longer required.
In versions of PHP from 5.0 to 5.1.3, the use of var was considered deprecated and would issue an E_STRICT warning, but since PHP 5.1.3 it is no longer deprecated and does not issue the warning.
If you declare a property using var instead of one of public, protected, or private, then PHP 5 will treat the property as if it had been declared as public.


Read More

PHP Code:
class tutClass {
    public 
$forum "SupportForums.net"// var/public defined attributes can be accessed everywhere 
    
    
private $admin "Omniscient"// private defined attributes may only be accessed only by the class that defined the member
    
    
protected $area "Administrator Panel"// protected defined attributes can only be accessed within the (parent) class it self

    
const constant "Class"// define a constant value
    
public static $static "Static value"// define static value
 


Variables should be clear by now to you, so I will cover an CONSTANT a little.
Constant is an name or identifier for a value, the value of an constant can't change during the execution of the script.
Constants doesn't need the $ sign as variables when defining and accessing.
As variables and function names, constants are case-sensitive.

Static properties or methods can be accesssed without the need to initialize the class first.

Methods:
Methods are functions within your class, they are defined as properties with the keywords,
public, private, protected and the keyword function.

PHP Code:
class tutClass {
    public 
$forum "SupportForums.net"// var/public defined attributes can be accessed everywhere 
    
    
private $admin "Omniscient"// private defined attributes may only be accessed only by the class that defined the member
    
    
protected $area "Administrator Panel"// protected defined attributes can only be accessed within the (parent) class it self

    
const constant "Class"// define a constant value
    
public static $static "Static value"// define static value

    // function is same as public function
    
public function foo() {
        
// function code
    
}

    private function 
bar() {
        
// private function is same as private variable 
        //and can only be executed within the class itself
    
}
 


Access
To access the properties and methods of your class, you need to create an instance Object of your class.
The instance of an Object is created with the keyword new, following by the name of your Class.

PHP Code:
class tutClass {

}

$obj = new tutClass

Within the class all preoprties and methods are accessed using the pseudo-variable $this, outside the class $this is represented
by the instance variable, in the above code that would be the $obj variable.
Following by -> and the name of the property or method, $this->admin would have the value "Omniscient".

PHP Code:
class tutClass {
    public 
$forum "SupportForums.net"// var/public defined attributes can be accessed everywhere 
    
    
private $admin "Omniscient"// private defined attributes may only be accessed only by the class that defined the member
    
    
protected $area "Administrator Panel"// protected defined attributes can only be accessed within the (parent) class it self

    
const constant "Class"// define a constant value
    
public static $static "Static value"// define static value

    // function is same as public function
    
public function foo() {
        
$this->bar(); // call the function bar
    
}

    private function 
bar() {
        echo 
$this->area// output: Administrator Panel
        
echo $this->admin// output: Omniscient
    
}
 


If you try to access your properties without the pseudo-variable, you will get "undefined variable" error.
Let us create an instance.


PHP Code:
$obj = new tutClass;

echo 
$obj->forum// output: SupportForums.net
echo $obj->admin// will output fatal error because the property $admin was defined as PRIVATE
echo $obj->area// will output fatal error because the property $area was defined as PROTECTED

$obj->bar(); // fatal error the method is defined as private

// but if we call the method foo(); we will get the out but of bar();
$obj->foo(); // output: Administrator Panel, Omniscient 

We have created two more properties, constant and static, those properties cannot be accessed with the pseudo-variable but only with the;

Scope Resolution Operator "::":
Read More.

Constant and Static values:
To access those values the are following forms.
Form to access constants inside your class is: self::constant, parent::constant .
Form to access static inside your class is: self::$static, parent::$static .

Outside of your class those two values can be accessed with the name of your Class.
In my tutorial the form would be: tutClass::constant and for static tutClass::$static

PHP Code:
$obj = new tutClass;

echo 
$obj->constant// this will output an fatal error
echo $obj->$static// this will output an fatal error
// right way

// const constant = "Class";
echo tutClass::constant// output: Class 

// public static $static = "Static value";
echo tutClass::$static// output: Static value 

Constant and static values doesn't require an instance of your class, in other words the line from the above script isn't needed.
PHP Code:
$obj = new tutClass

Extending your class:
I will cover one more feature, you can combine two classes to work together and use each others properties and methods.
That is being done when defining your class, with the keyword extends following by the name of the class that should be used.

PHP Code:
class FOO {
    public 
$foo "First class";
    const 
Bar "Constant";
}

class 
BAR extends FOO {
    public function 
showFOO() {
        echo 
$this->foo// output: First Class
        
echo parent::Bar// output: Constant
    
}


As you can see in the above code, we are able to access properties and methods from one class within an other class.
parent is used to access constant or static values from the class that is being imported in your class with the extends keyword.


The following code will execute the function "showFOO()" from our BAR class, which then outputs properties from our extended class, public $foo and const Bar.

PHP Code:
$obj = new BAR;
$obj->showFOO(); 


Constructor:
You are able to define an constructor for your class, a constructor is run at each newly-created instance of your class.
Constructor mostly contain code to initialize the class and define needed values.

PHP Code:
class tutClass {
    public 
$id;

    public function 
__construct($id) {
        
$this->id $id;
    }
}

$obj1 = new tutClass(1); // $this->id is then defined in the __construct() as 1

$obj2 = new tutClass(2); // $this->id is then defined in the __construct() as 2 

There are also;

Destructors:
I guess that you can imagine what they are used for.

http://www.php.net Wrote:The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.

PHP Code:
class tutClass {
    public 
$id;

    public function 
__construct($id) {
        
$this->id $id;
    }

    public function 
__destruct() {
        unset(
$this->id);
        echo 
"Class destroyed!";
    }


Read More
PHP OOP

Thank you for reading!Blackhat
Reply
#2
Fantastic, I learnt many new points which I can't know before. Thanks Ninja
Reply
#3
(11-10-2009, 01:51 AM)zone Wrote: Fantastic, I learnt many new points which I can't know before. Thanks Ninja

Thank you very much, Blackhat
I'm glad you learned something. Big Grin
Reply
#4
Thanks for this, I was actually needing something like this almost at the very moment you posted it.

Id rep you if i could Big Grin
LockerZ Invite Required? PM Me.
Visit TechBeat for the latest Technology News
[Image: jamzasigniture.jpg]
Reply
#5
(11-10-2009, 04:48 AM)Jamza Wrote: Thanks for this, I was actually needing something like this almost at the very moment you posted it.

Id rep you if i could Big Grin

Thank you too, and don't worry about REP. Blackhat
I'm glad if I helped you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 941 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,667 10-10-2011, 01:00 PM
Last Post: Greyersting
  PHP Beginners guide Gaijin 40 16,023 08-30-2011, 08:31 AM
Last Post: Slash
  [Guide]The Absolute Bare Foundations Of PHP[Free] Poppins 6 1,516 12-14-2010, 05:53 PM
Last Post: Buzz Lightyear

Forum Jump:


Users browsing this thread: 2 Guest(s)