PHP class using example code

  • 2020-03-31 19:45:43
  • OfStack

PHP has only categories (classes), methods, properties, and single extensions. To be not used to use C++, Java, Delphi and other object-oriented language to develop the program of the user, might as well read the object-oriented concept of the book, I believe that can bring a lot of harvest.
The following example is a trolley class. As you can see, the use of class indicates that it is a class category. Function in a category, such as add_item, represents a method of that class. Methods can encapsulate the actual handling of a class so that the class itself can perform some steps following the encapsulated methods.

The $this class variable in the program is also a special variable in PHP, like $GLOBALS and $php_errormsg. The $this variable is used only in the class category to represent the class itself.
 
<?php 
//Program name: cart. Inc
class Cart { 
var $items; //The cart class

//This method adds $num items to the cart (add to the $artnr variable)
function add_item ($artnr, $num) { 
$this->items[$artnr] += $num; 
} 

//This method reduces $num items from the cart (subtracted from the $artnr variable)
function remove_item ($artnr, $num) { 
if ($this->items[$artnr] > $num) { 
$this->items[$artnr] -= $num; 
return true; 
} else { 
return false; 
} 
} 
} 
?> 


You can use a trolley in a similar way. You can save each class as an Include file and then require or Include it. When you define the variable $cart, you use the reserved word new to indicate that $cart USES the cart class. Use - > Symbol for a method that executes a class.
 
<?php 
require("cart.inc"); 
$cart = new Cart; 
$cart->add_item("10", 1); 
?> 


And then the design of a named cart. Named trolleys are inherited from trolleys, so there are methods and attributes that the trolleys possess, and named trolleys have methods that add a name to the trolleys.

As you can see from the following example, the subclass Named_Cart USES extends to inherit from its superclass Cart. Although there are no methods to add or subtract items from the Named_Cart class, it has everything the parent class has due to its genetic nature.
 
<?php 
//Program name: named_cart. Inc
require("cart.inc"); 
class Named_Cart extends Cart { 
var $owner; 
function set_owner ($name) { 
$this->owner = $name; 
} 
} 
?> 


To use the named cart class, see the following example. This is not a good design, of course, because each subclass requires its parent all the time, placing a burden on the server over I/O. At implementation time, you can put the entire family of classes in the same program file, from the earliest to the last descendant, and also for later modification.
 
<?php 
require("named_cart.inc"); 
$ncart = new Named_Cart; //Create class variables
$ncart->set_owner ("CyberRidder"); //Configure the name attribute for the class
echo $ncart->owner; //Displays the named properties of the class
$ncart->add_item ("10", 1); //Methods inherited from parents can also be used
?> 


As a result, the use of the extends reserved word in PHP, coupled with good system analysis and a full CRC card design (see the object-oriented book), can turn PHP into a CGI language with powerful class capabilities.

Because PHP is a scripting language (Script), so the program source code is visible, components in the software project black box will not appear in the current version of PHP, that is, all the classes do not actually hide its content. For the software industry, there is no way to protect the so-called software IC, standing in the Open community, but the Source code is a good thing, as to which is right or wrong, it is difficult to determine, but currently PHP is still a member of the Open Source community, perhaps in the future Zend engine can do the function of class encapsulation is not certain.

Related articles: