OOP using PHP

Extending Class

<?php

/*** include the vehicle class definition ***/
include(‘OOP.php’);

class motorcycle extends vehicle{

/*** the number of side cars ***/
private $num_sidecars;

private $handlebars;

/**
*
* set the type of handlebars
*
* @access public
*
* @param string
*
* @return string
*
**/
public function setHandlebars($handlebarType){
$this->handlebars=$handlebarType;
}

/**
*
* Set number of side cars
*
* @access public
*
* @param int
*
* @return int
*
**/
public function setSidecar($numSidecars){
$this->numSidecars = $numSidecars;
}
/**
*
* Show the numbers of sidecars
*
* @return string
*
**/
public function showSideCar(){
echo ‘This motorcyle has ‘. $this->numSidecars.’ sidecar<br />’;
}

} /*** end of class ***/
/*** our userland code ***/

/*** create a new vehicle object ***/

echo “<br><br>”;
$vehicle = new motorcycle;

/*** the brand of vehicle ***/
$vehicle->brand = ‘Harley Davidson’;

/*** the shape of vehicle ***/
$vehicle->shape = ‘Sportster’;

/*** the color of the vehicle ***/
$vehicle->color = ‘Black’;

/*** number of doors ***/
$vehicle->num_doors = 0;

/*** cost of the vehicle ***/
$vehicle->price = 25000;

/*** type of handle bars ***/
$vehicle->setHandlebars(‘Ape Hangers’);

/*** set the sidecar ***/
$vehicle->setSidecar(1);

/*** show the vehicle brand and type ***/
echo $vehicle->brand.’: ‘.$vehicle->shape.'<br />’;
/*** call the showPrice method ***/
$vehicle->showPrice();

/*** show the sidecars ***/
$vehicle->showSideCar();

/*** drive the vehicle ***/
$vehicle->drive();

?>

mysqli

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
</head>

<body>
<?php
$mysqli = new mysqli(“localhost”, “root”, “root”, “test”);

/* check connection */
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}

$query = “SELECT * from student;”;
$query .= “SELECT count(*) FROM student”;

/* execute multi query */
if ($mysqli->multi_query($query)) {
do {
/* store first result set */
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_row()) {
printf(“%s\n”, $row[0]);

if(isset($row[1]))
{
printf(“%s\n”, $row[1]);
}
echo “<br/>”;

}
$result->free();
}
/* print divider */
if ($mysqli->more_results()) {
printf(“—————–\n<br/>”);
}
} while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>

</body>
</html>

 

OOP

<?php
class vehicle{
/*** define public properties ***/

/*** the color of the vehicle ***/
public $color;

/*** the number of doors ***/
public $num_doors;

/*** the price of the vehicle ***/
public $price;

/*** the shape of the vehicle ***/
public $shape;

/*** the brand of vehicle ***/
public $brand;

/*** the constructor ***/
public function __construct(){
echo ‘About this Vehicle.<br />’;
}

/*** define some public methods ***/

/*** a method to show the vehicle price ***/
public function showPrice(){
echo ‘This vehicle costs ‘.$this->price.’.<br />’;
}

/*** a method to show the number of doors ***/
public function numDoors(){
echo ‘This vehicle has ‘.$this->num_doors.’ doors.<br />’;
}

/*** method to drive the vehicle ***/
public function drive(){
echo ‘VRRROOOOOOM!!!’;
}

} /*** end of class ***/

$vehicle = new vehicle;

/*** the brand of vehicle ***/
$vehicle->brand = ‘Porsche’;

/*** the shape of vehicle ***/
$vehicle->shape = ‘Coupe’;

/*** the color of the vehicle ***/
$vehicle->color = ‘Red’;

/*** number of doors ***/
$vehicle->num_doors = 2;

/*** cost of the vehicle ***/
$vehicle->price = 100000;

/*** call the showPrice method ***/
$vehicle->showPrice();

/*** call the numDoors method ***/
$vehicle->numDoors();

/*** drive the vehicle ***/
$vehicle->drive();

?>

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.