Free Computer Books

   »    Free Computer Books on Computer Programming, Web Development & Designing

Computer Education, Training & Tutorial Resources - ComputerEducationWorld.com
Home » Free Computer Books »

3.2 OBJECTS
The main difference in OOP as opposed to functional programming is that the
data and code are bundled together into one entity, which is known as an
object. Object-oriented applications are usually split up into a number of
objects that interact with each other. Each object is usually an entity of the
problem, which is self-contained and has a bunch of properties and methods.
The properties are the object’s data, which basically means the variables that
belong to the object. The methods–if you are coming from a functional back-
ground–are basically the functions that the object supports. Going one step
further, the functionality that is intended for other objects to be accessed and
used during interaction is called an object’s interface.

A class is a template for an object and
describes what methods and properties an object of this type will have. In this
example, the class represents a person. For each person in your application,
you can make a separate instance of this class that represents the person’s
information. For example, if two people in our application are called Joe and
Judy, we would create two separate instances of this class and would call the
setName() method of each with their names to initialize the variable holding
the person’s name, $name. The methods and members that other interacting
objects may use are a class’s contract. In this example, the person’s contracts
to the outside world are the two set and get methods, setName() and get-
Name().
class Person
Methods:
setName($name)
getName()
Properties:
$name
Fig. 3.1 Diagram of class Person.
The following PHP code defines the class, creates two instances of it, sets
the name of each instance appropriately, and prints the names:
class Person {
private $name;
function setName($name)
{
$this->name = $name;
}
function getName()
{
return $this->name;
}
};
$judy = new Person();
$judy->setName(”Judy”);
$joe = new Person();
$joe->setName(”Joe”);
print $judy->getName() . “\n”;
print $joe->getName(). “\n”;


• • •
 

3.1 INTRODUCTION
PHP 3 is the version that introduced support for object-oriented programming
(OOP). Although useable, the support was extremely simplistic and not very
much improved upon with the release of PHP 4, where backward compatibil-
ity was the main concern. (more…)


• • •
 

2.8.1 User-Defined Functions
The general way of defining a function is
function function_name (arg1, arg2, arg3, …)
{
statement list
}
To return a value from a function, you need to make a call to return expr
inside your function. This stops execution of the function and returns expr as
the function’s value.
The following example function accepts one argument, $x, and returns its
square:
function square ($x)
{
return $x*$x;
}
After defining this function, it can be used as an expression wherever you
desire.
For example:
print ‘The square of 5 is ‘ . square(5);

(more…)


• • •
 

Operator
Name

Value
==
Equal to
Checks for equality
between two arguments
performing type conver-
sion when necessary:
1 == “1″ results in true
1 == 1 results in true
!=
Not equal to
Inverse of ==.
>
Greater than
Checks if first operand is
greater than second
<
Smaller than
Checks if first operand is
smaller than second
>=
Greater than or equal to
Checks if first operand is
greater or equal to second
<=
Smaller than or equal to
Checks if first operand
is smaller or equal to
second
For the following two operators, automatic type conversions are not per-
formed and, therefore, both the types and the values are compared.
Operator
Name
Value
===
Identical to
Same as == but the types
of the operands have to
match.
No automatic type conver-
sions are performed:
1 === “1″ results in
false.
1 === 1 results in true.
!==
Not identical to
The inverse of ===.

(more…)


• • •
 



captcha PHP Script Free PHP captcha script free php
Website Design by WebWalas.com