Vous êtes sur la page 1sur 1

(Chapter 7) PHP Objects (Code optimising and classes)

Programming Objects (OOP)

First , object oriented programming is a method of constructing a program. An object itself is more of a concept than
an actual data type of a programming language. An object for most languages is a device with can be made packets
of code with sets of procedures/methods/functions that relate their class or object name, of the class or object
name, and relates the general purpose of the packeted code sets.
PHP objects are called classes.and they have an ability to be extended similar to the purpose and other classes
that Java2 or C++ has of abstract classes. Again a PHP class may or may not have a special function-like system
called a constructor.

Basic PHP class system

Aside to similarities of other languages (if they help you) PHP has its own layout of these systems and associate operators
for handling objects and one or two functions designed specifically for PHP operations with objects.
To refer to a class that has not been instantiated (created) an operator :: (double colon) is used between the class name
to be called(as instance in runtime code) and the function to be called from that class.
In an extended class the parent class can be called using the parent keyword in front of the :: double colon operator.
To refer to an instance of a variable inside a class and call a function to set it when the variable could be existent
in other copies of the class also existent at the time, the variable is preceded by a special variable and symbols $this->
e.g. $this->browser_name = get_browser().
Variables for the class declared in the global scope of the class also require the var keyword preceding them
and are not given a setting but declared with no value and no equal sign.Normally this condition causes a warning from the
runtime parser if there is no assigned value to a variable but not with the internal global variable of a class. To instantiate
a variable of a class a constructor function must be used or the variables value can be set by calling a function inside the
class that is designed to either return the variable with a value(e.g. numeric) or assign it(e.g. string).
To create a substantiated individual reference of a class requires
using a variable and the new keyword.
Some names for functions are reserved in PHP for special purposes and cannot be used by the programmer except for
their designed purpose e.g. _sleep and _wakeup or serialize() and unserialize() object copy passing. This last set of
actions is a little complex here and also the & operator for reference copies of objects and parts that operates a little
similar to C/C++ or Fortran pointers so they will not be explained here in this tutorial.

<?php
############################ basic-class.php ######################
class Printer{
#
function prnt($inout){
print($inout);
}
#
function prnArr($aroutr){
print_r($aroutr);
}
#
}
# end class
class ClientInf extends Printer{
var $refpage;
var $OpSys;
var $arr=array("the","above","are","user agent","at top","and","the","referer","page","below"); # direct assign is allowed with arrays
function ClientInf(){
$this->refpage=$_SERVER['HTTP_REFERER']; # assigning the value using the class constructor
# get_browser(null,true); # is an array AND NOT SET IN THIS SERVER AS A FEW OTHER HEAD BANGERS FOR NEEDS
$this->OpSys=$_SERVER['HTTP_USER_AGENT']; # assigning the value using the class constructor
}
# end constructor
function prn($strprn){
parent::prnt($strprn);
}
function envprn($arrayoutprn){
parent::prnArr($arrayoutprn);
}
}
# end class
$clie_out=new ClientInf();
$clie_out->prn("<p><p>".$clie_out->OpSys."<p><p>");
$clie_out->prn("<p><p>".$clie_out->refpage."<p><p>");
$clie_out->envprn($clie_out->arr);
# printer class itself in this instance of use (below) has not been created at time of call
Printer::prnt("<p><p>".$clie_out->refpage.'<br>direct call to Printer class<p>');
print('<p><p>');
Printer::prnArr($clie_out->arr);
print('<br>direct call to Printer class<p>');
?>

Vous aimerez peut-être aussi