Object-oriented programming on PHP. Classes

My way to understanding of objects went too for a long time. It is necessary to say, that he has ended extremely unexpectedly - I have read... Manual PHP 4. Where it is possible to find the explanatory{sensible} description, only not there, it would seem... To tell the truth, already before I knew something ("... The object combining both a data set, and actions above them. " (s) Epaneshnikov, " Programming in Turbo Pascal 7.0" environment), but it already details.


What is the class and object. At first about object. Definition "... Combining both a data set, and actions above them " - quite suitable. If to speak "ordinary" the object in PHP is a variable of special type. In her contain specially declared under - variable and functions of this object (that the object contains variables and functions, in the scientific literature is called inkapsuljaciej). Function is_object on this variable gives out true:



if (is_object ($objectname)) {

  do_something ();

};


The reference{manipulation} to under - variable object is made as follows (the name, certainly wrong, " property of object " is correct).



$objectname-> property

print ($objectname-> property);

// A call of function (method) of object:

$objectname-> format_output ($format);


It is certainly inconvenient to write a name of object and "strelochku" ("-> ") before the necessary variable, but it only firstly. But then it is possible to save great volume of a program code (and to avoid a superfluous headache).


Now that such a class. The class - means a class of objects. In PHP-scripts the object is described not. First the class of objects is described, and then it is possible to create some objects of this class.



<?

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 ". Final. ";

    else

      echo " following - ", $this-> stop [$this-> current_stop+1];

}


  function stop () {

    $this-> passengers + = intval (rand ((-1 * $ this-> passengers), 100));

    if ($this-> passengers> $this-> capacity) {

      echo " we Release{Exempt} doors! ";

      $this-> passengers = $this-> capacity;

};

}


  function go () {

    $this-> current_stop ++;

}

}

?>


ATTENTION! The closing bracket of a class should be without a semicolon (" "), as well as all descriptions of functions inside the description of a class.


The program working with class Obhhestvennyj_Transport will look so:



<?

$bus = new Public_Transport;

$bus-> capacity = 200;

$bus-> vehicle = "Liaz-767";

$bus-> stop = array ("Shopping center", "Polyclinic", " Institute of thermophysics ",

"Computer center", " Institute of nuclear physics ", " Institute of hydrodynamics ",

" The sea prospectus ", " the House of scientists ", " street. Pearl ", " the Color fare ");


while ($bus-> current_stop <sizeof ($bus-> stop)) {

  $bus-> say_stop ();

  $bus-> stop ();

  $bus-> go ();

};

?>


In this example one bus is started only, and it is possible both two, and three, and as much as necessary. Understandably, what is it it is possible to repeat and without the help of objects, but it is more complex , and the received code not so is easily read, how with objects, especially, when it is a little bit{some} "subjects".


Object and his  properties are usual variables. For example, it is possible to work with dynamic names of variables:



<?

$a = " bukva a ";

$b = " bukva b ";

$c = "a";

echo $$ c;

?>


Such code will deduce{remove} " bukva a ". And it is possible to do{make} the same with objects and their properties:



<?

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};

?>


(such code will give out "3")


The same concerns also dynamic names of functions.


In a management{manual} on PHP4 it is written more in detail about dinamichesikh names variable and changeable names of functions.


And at parting that. In the beginning of year I should write a script for newsfeed and price-lists to subscribers. I have gone on site PHP and have had a look in a manual on function mail () to find something about attachment. In comments to function I have found that searched - for a class for an investment of a file in the letter. For eight months there have thrown many links to such classes, and in February he was unique - CMailFile. And so, as they do{make} - to do{make} it is not necessary (I then understood classes vaguely, and simply cut out functions, a little having simplified a code). I shall quote headings of functions:



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 will be the designer of a class */


  function attach_file ($filename, $mimetype, $mime_filename) {

/* It I do not understand! attach_file it is caused from function CMailFile - what for?

Only for appearance. And so - it was possible to insert this piece of a code directly in main

Function, time is solved to make a lumpsum call of function. Further go a little

Functions of the same purpose  and character. */

  function encode_file ($sourcefile) {

  function sendfile () {

  function write_mimeheaders ($filename, $mime_filename) {

  function write_smtpheaders ($addr_from) {


}


/* And an example of use of a class. */


// usage - mimetype example "image/gif"

// $mailfile = new CMailFile ($subject, $sendto, $replyto, $message, $filename, $mimetype);

// $mailfile-> sendfile ();


What for was to make out it as a class - not clear. Only for appearance and divisions of function into some pieces. And generally, if I shall want on the move to change the addressee (for example, for the same dispatch when I pass a cycle on a file of addresses), it is necessary to cause again function CMailFile which will recode a file again, demanding the certain system resources.


Time I started to abuse another, I shall show, as in my opinion to use OOP for dispatch of letters more correctly. In the following release devoted to receptions OOP (and before, I I think, to it{him;them} budud others two) I want to give an example uses of a class for sending post messages, including for dispatches.





© Web Development Company Conkurent, LLC 2008-2009. All rights reserved.