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
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.
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.
Read More
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.
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.
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".
If you try to access your properties without the pseudo-variable, you will get "undefined variable" error.
Let us create an instance.
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
Constant and static values doesn't require an instance of your class, in other words the line from the above script isn't needed.
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.
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.
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.
There are also;
Destructors:
I guess that you can imagine what they are used for.
Read More
PHP OOP
Thank you for reading!
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!