1 / 25

Object Oriented Programming in PHP

Object Oriented Programming in PHP. List of Items of Interest. What is a Class What is an Object What is a Singleton Multiple Objects, How to store. Advantages and Disadvantages of OOP Parts of a Class Properties Methods Constructors Creating an Object

hong
Download Presentation

Object Oriented Programming in PHP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming in PHP

  2. List of Items of Interest • What is a Class • What is an Object • What is a Singleton • Multiple Objects, How to store. • Advantages and Disadvantages of OOP • Parts of a Class • Properties • Methods • Constructors • Creating an Object • Passing data to an object when it is first created or to the constructor • Passing data to a property • Passing data to a method • Getting data from a property • Getting data from a method • Naming issues/requirements • Printing • PDO vsmysql_link • Sending multiple variables of data back from a method • Parsing of the returned data

  3. What is a Class • A Class is a template of code that is used to create an Object. • Objects do the work, Classes are the design of the code to do the work.

  4. What is an Object • An Object is a Instantiation of a Class. • If you have only one Object from a class it is called a Singleton • If you have multiple Objects from a class it is best to store the objects in an array.

  5. Advantages and Disadvantages of OOP • If you have a large project in PHP or if you have a project that is in a constant state that it is running (other then PHP) then you should use OOP. • If you have a small project that does not use a lot of reused code then Procedural programming is better as it is lighter.

  6. Parts of a Class • Properties • If you have variables that are to be shared between methods it is best to declare them as properties. • As of PHP5 properties start with private, public or protected • The visibility of private, public or protected is different then ActionScript • public $name = “Doug”; • They are reference with $this->name in methods • They are references from the main file with • $oObject->name ;

  7. Parts of a Class • Methods • Methods are functions that work on data. • As of PHP5 methods start with private, public or protected • The visibility of private, public or protected is different then ActionScript • public function myToDo () { } • They are reference with $this->myToDo () in other methods • They are referenced from the main file with • $oObject->myToDo () ;

  8. Parts of a Class • Constructors • Constructors are special methods that are called when an Object is created. • As of PHP5 Constructors are named as __construct() • The visibility of private, public or protected is different then ActionScript • public function __construct () { } • I usually use the constructor to set default properties for the object such as connecting to the Database.

  9. Creating an Object • You first need to include the Class with a require_once to reference the file the class is in. • I always start the name of my objects with a lowercase o and then camel case the rest of the name. $oObject • Use the new command to create the object $oObject = New Bicycles();

  10. Passing data to an object when it is first created or to the constructor • When creating the object from the class pass variable data in the brackets to the constructor. • $oObject = New Bicycles($id); ------------------------- Class Bicycles { public $id = “”; public function __construct ($id) { $this->id = $id; } }

  11. Passing data to a property $oObject->id = $id; // outside object $oObject->id = “22”; // outside object $this->id = $id; // from within object $this->id = “22”; // from within object

  12. Passing data to a method • Data id passed in the brackets of the method name; $oObject->getBicycle($id); // outside object $oObject->getBicycle(“22”); // outside object $this->getBicycle($id); // from within object $this->getBicycle(“22”); // from within object

  13. Getting data from a property $id = $oObject->id ; // outside object print $oObject->id ; // outside object $id= $this->id; // Do not do this as you just need to reference the property only from within object

  14. Getting data from a method $title = $oObject->getBicycleTitle($id); // outside object print $oObject->getBicycleTitle($id); // outside object $title = $this->getBicycleTitle($id); // from within object (Never Print from with in a Method except for debugging)

  15. Naming issues/requirements • When naming properties and methods use camel case • When naming variables use lowercase with underscores. • The use of objects reduces name space issues • Start all class names with a capital • Start all objects with a lower case o

  16. Printing • NEVER use print or echo with a class or object • (printing can only be used for debugging) • It is bad form • data does not print where it needs to be seen.

  17. PDO vsmysql_link • The procedural way of connecting to a database is to create a resource link such as $mysql_link • For OOP use the PDO way which is PHP Database Objects • Create a property in the __construct that has the reference to the PDO. public $pDbh ; // define constructor public function __construct() { require_once("connect_dbh.php"); $this->pDbh = &$dbh; }

  18. PDO vsmysql_linkconnect_dbh.php <?php try { $user = ”youraccount"; $pass = ”yourpassowrd"; $dbh = new PDO('mysql:host=localhost;dbname=yourdatabase', $user, $pass); } catch (PDOException $e) { print("Error: ".$e->getMessage()."<br /> "); } ?>

  19. PDO vsmysql_linkSELECT $query = "SELECT title FROM bicycles WHERE id = '$id'"; foreach ($this->pDbh->query($query) as $row) { $title = stripslashes($row[0]); }

  20. PDO vsmysql_linkINSERT, DELETE or UPDATE $query = ”UPDATE bicycles SET title = ‘$title’ WHERE id = '$id'"; $this->pDbh2->query($query);

  21. Sending multiple variables of data back from a method • You can use a return command to send a single variable back from the calling method. public function addThis ($a,$b) { $total = $a + $b; return $total; }

  22. Sending multiple variables of data back from a method • To send multiple variables back in a return you need to place the variables into an array. public function getBicyclesList () { $data = “”; $query = "SELECT id, title FROM bicycles ORDER BY id ”; $x = 0; foreach ($this->pDbh->query($query) as $row) { $id= stripslashes($row[0]); $title = stripslashes($row[1]); $data[$x][‘id’] = $id; $data[$x][‘title’] = $title; ++$x; } return $data; }

  23. Parsing of the returned data • If the data is a simple structure then just print it or place into a variable. print $oObject->addThis(12, 6) ; // prints 18

  24. Parsing of the returned data • If the data is an array structure then you need to parse the array. $bikeListArray = $oBicycles->getBicyclesList(); foreach ($bikeListArray as $key=>$bike) { $id = $bike[‘id’]; $title = $bike[‘title’]; print(“<option value=\”$id\”>$title</option>”); }

  25. File Names • Each class resides in it’s own file • Each file has the name class in it. • The file begins with the name of the class bicycles.class.php These files will reside in the inc/php folder

More Related