Object – oriented programming, classes
What does “class” and “object” terms mean? At first let’s talk about object. Object in PHP is a special type variable. It contains specially declared sub-variables and functions of that object. Function is_object for that variable returns true:
if (is_object($objectname)) {
do_something();
};
The object sub-variable calling is follows:
$objectname->property
print ($objectname->property);
//Function (method) cal:
$objectname->format_output($format);
What is the class? Class means object class. Object isn’t described in the PHP scripts. At first there is described the object class, only after that there can be created any amount of the objects:
<?php
class Public_Transport {
var $capacity = 0;
var $passengers = 0;
var $stop = array();
var $current_stop = 0;
var $vehicle = "unknown";
function say_stop () {
echo $this->stop[$this->current_stop];
if ($this->current_stop==sizeof($this->stop)-1)
echo ". route terminal.";
else
echo " next - ", $this->stop[$this->current_stop+1];
}
function stop () {
$this->passengers += intval(rand((-1*$this->passengers),100));
if ($this->passengers > $this->capacity) {
echo "Release the doors!";
$this->passengers = $this->capacity;
};
}
function go () {
$this->current_stop++;
}
}
?>
ATTENTION: Right bracket should be written without semicolon (“”).
Program that works with “Public transport” class looks as follows:
<?php
$bus = new Public_Transport;
$bus->capacity = 200;
$bus->vehicle = "A-767";
$bus->stop = array ("Trade center", "Polyclinic", "Institute of thermal physics ",
"omputation center", "Institute of the nuclear physics", "Institute of hydrodynamics",
);
while ($bus->current_stop < sizeof($bus->stop)) {
$bus->say_stop();
$bus->stop();
$bus->go();
};
?>
There is launched only one bus. It can be repeated without objects but it is more complicated.
Object and its variables are common variables. For example we can work with dynamic variable names:
<?php
$a = "letter a";
$b = "letter b";
$c = "a";
echo $$c;
?>
Such code will print: “letter a”. Just the same can be done with objects and their properties.
<?php
class someclass1 {
var $a = 1;
var $b = 2;
var $c = 3;
}
class someclass2 {
var $a = 4;
var $b = 5;
var $c = 6;
}
$d = new someclass1;
$e = new someclass2;
$f = "d";
$g = "c";
echo ${$f}->{$g};
?>
That code will print “3”.
Once I visited the PHP site for searching information about attachment. At last I found that I wanted – class for attaching file to the letter. Don’t repeat their mistakes. Here are the function headers:
class CMailFile {
var $subject;
var $addr_to;
var $text_body;
var $text_encoded;
var $mime_headers;
var $mime_boundary = "--==================_846811060==_";
var $smtp_headers;
function CMailFile($subject,$to,$from,$msg,$filename,
$mimetype = "application/octet-stream", $mime_filename = false) {
/* if function has the same name as a class, it is the class constructor */
function attach_file($filename,$mimetype,$mime_filename) {
/* I don’t understand it! attach_file is called from the CMailFile function – what for?
function encode_file($sourcefile) {
function sendfile() {
function write_mimeheaders($filename, $mime_filename) {
function write_smtpheaders($addr_from) {
}
// usage - mimetype example
// $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filename,$mimetype);
// $mailfile->sendfile();


