PHP - Part3
PHP - Part3
Today's Goals
Today we will begin with a discussion on
objects in PHP including how to create
instances and custom objects.
Object-Oriented Programming.
This will be followed by a discussion of
XAMP/WAMP environment.
Objects in PHP
The concept of objects is the same across
different object-oriented programming
languages.
There are, however, minor differences in
how a programmer references objects
using PHP.
Creating a New PHP Object
Instance
Just like JavaScript, PHP uses the keyword
"new" to create a new instance of an object.
Example: $_myinstance = new
Object(args);
Syntax elements:
Just like variables, the name used to identify
the instance needs to begin with '$'.
Many objects need arguments (the "args" part
of the above example) in order to create a new
instance. These are passed to a function
called a constructor which initializes the
instance.
Referring to Components of a PHP
Instance
In JavaScript, we used periods (.) to
delimit/separate the elements of an object
hierarchy. For example:
document.writeln("Hello, World!");
In PHP, the operator "->" is used to delimit/separate
the elements of an object hierarchy. For example:
$object_name->object_function();
The above example refers to a function (note the
parenthesis). The same format is used for
properties too, i.e.,
$object_name-> property;
Defining a Class
class ClassName
{
// Member variables
var $_variable1 = 0;
var $_variable2 = "String";
// Member functions
function classFunction($_arg1 = 0, $_arg2)
{
// Function code goes here
}
}
?>
Format of a Class Definition
(continued)
The keyword "class" followed by the class
name is used to start the definition. Curly
brackets are used to enclose all of the
elements of the definition.
The keyword "var" is used to identify the
class' variables.
Variables can be initialized. Every time a
new instance is created, the variables for
that instance are initialized to these values.
Functions are defined normally, but when
contained within the curly brackets of the
class, become member functions of the class.
Private Member Variables
There are some cases when a class may not
want to have its variables accessible outside of
the class
Variables may be set up only for internal use within
the class' functions
Variables may have certain restrictions on values that
must be enforced internally
If a variable needs to be modified from outside
the class, a function can be provided to do so.
For example, instead of:
$_instance -> variable1 = 25;
use
$_instance -> updateVariable1(25);
Private Member Variables
To declare a variable as private, simply replace the
keyword "var" with the keyword "private" in the
variable declaration.
Example:
private $_variable3 = 4.0;
A class can also have private member functions. In
this case, declare the function by putting the
keyword "private" in front of the function
declaration.
Private variables became available with PHP 5.
Static Member Variables
Each time an instance of a class is created, a
whole new set of variables and functions for that
instance is created along with it.
It is possible to make it so that regardless of the
number of instances of a class, only a single
variable is created for that class.
This allows all instances to share a single variable.
To do this, replace the keyword "var" with the
keyword "static" in the variable declaration.
Static variables became available with PHP 5.
Constructors
A function can be written for a class that is
automatically called whenever an instance for
that class is created. This is called a constructor.
class ClassName {
data member and member function definitions
}