Jump to content


- - - - -

[PHP] Object Oriented


  • Please log in to reply
5 replies to this topic

#1 sharkbate24

sharkbate24

    Private Pilot - IFR

  • Members
  • PipPipPipPip
  • 943 posts

Posted 16 July 2009 - 02:25 PM

Hello everyone,

A lot of PHP programmers tend to program their scripts the 'old' style, which is procedural. There's nothing wrong about this, in fact, this is perfect for small PHP programs, but Object Oriented can offer a lot to programmers writing big scripts. I'm not going to go through the advantages / disadvantages etc as I just want to teach you OOP, but just make sure your projects need OOP before actually using OOP in your project as OOP can sometimes use up more programming than procedural in small projects.

Objects are basically the building blocks of a program. Think of objects as bricks - they all join together to make a whole building, in our case, a software or program.

Before you can create objects, you need to design a blueprint (what the object is made from). To do this, we make a class. To make this easier, I'm going to call the class 'myClass' and relate to it as a class in school, and the functions inside the class are going to be what you find in a class. Inside a class we can have global variables, functiosn etc, functions being the neccessity for OOP to work.

Syntax for my example:
<?php
class myClass {
   function students() {

   }

   function books() {

   }
}
?>

I'm not going to write anything in the functions as I just want to show you how they work. So basically, we have functions inside the class. They work just like functions, can return things, take parameters etc.

You can put a statement into the function such as:

function students() {
   $student_one = "Jimmy Smith";
   $student_two = "Michael Dane";
   $student_three = "Ashley Wotsits";
   echo "We have the following students: $student_one, $student_two, $student_three in our class.";
}

To call the function, OUTSIDE THE CLASS, we have to create a NEW object (I will call it myObject) based on the blueprint (the class called myClass):

Quote

$myObject = new myClass();
// Now we have to call the function inside myClass
$myObject -> students();
// This should run the function called students in myClass and output what we put in the function.

I hope this helps!
Thanks!

#2 /home/fsw_member

/home/fsw_member

    Private Pilot - VFR

  • Members
  • PipPipPip
  • 418 posts
  • Location:/earth/

Posted 16 July 2009 - 05:50 PM

very nice.  I have started to use this in some of my projects. quite useful and much easier to find a block of code for editing.  

An example of a news script Ive written.

class Media{ // the file is also called media.php with NO caps in a folder called '_class'

	  function ShowNewsTitle() {
	  [PHP BLOCK]
	  }//End Function ShowNewsTitle


	   function ProcessNews() {
	  [PHP BLOCK]
	  }//End Function ProcessNews


}//End class Media

Then just call the objects in whatever file ie: index.php

<?php
//include connection file

include_once '_class/media.php';

$obj = newMedia();//This calls all the objects inside media.php and below print specific objects
?>

<html>
<body>
<?php $obj->ProcessNews();?>
<?php $obj->ShowNewsTitle();?>
</body>
</html>

Just a quick example that doesnt do it justice but you get the idea.  
Having said that I have run into problems further down the line as the system gets bigger.(but that may be typo's  :hrmm: )

#3 sharkbate24

sharkbate24

    Private Pilot - IFR

  • Members
  • PipPipPipPip
  • 943 posts

Posted 16 July 2009 - 07:14 PM

Glad you enjoyed the tutorial.

While this is a PHP Object Oriented tutorial, you can take what you learn from this tutorial to other programming languages that use OOP such as C#. Also, note that the functions inside the class are also known as the methods.

When I say you shouldn't use Object Oriented for small projects, I mean projects with about 3 or less pages; generally because you may have to write more script. Object Oriented is good for large projects, especially in a team environment as everyone can write their own classes, methods etc without actually affecting other peoples codes.

I just finished a PHP jobs system where theres an admin panel, gets jobs from the database, application status, application process etc. Ideally, this would have possibly been better with OOP, but I chose to do procedural as it's not too big of a project.

Good luck!

#4 /home/fsw_member

/home/fsw_member

    Private Pilot - VFR

  • Members
  • PipPipPip
  • 418 posts
  • Location:/earth/

Posted 17 July 2009 - 04:39 AM

I think the next step after learning the procedural & OOP ways of scripting for me is to study an MVC framework such as 'Zend', 'Php Cake' etc.  I think I will opt for 'CodeIgnitor' as it looks quite comfortable to understand & you can import the massive Zend library's.

Back to OOP.  From the very little I know as im just starting to implement this into my scripts now, it is important to tell new scripters thats structure is very important.  

An example would be your connection class & session & cookies must be placed first depending on the code your calling.

include_once '_class/connection.php';
include_once '_class/media.php';

///because our $->ShowNewsTitle() is using a mysql_query the connection must be placed before the media class, otherwise you will see some fatal errors:

CORRECT:
$obj = newConnection();
$obj-> Database();

$obj = newMedia();


WRONg:
$obj = newConnection();
$obj = newMedia();

$obj-> Database();
  


This question is directed @ you [sharkbate24]

Why do I have parsing errors in this connection class:


class Connection {


	  var $db_host = "";
	  var $db_user = "";
	  var $db_passowrd = "";
	  var $db_database = "";

	  function Database() {

	  $connect = mysql_connect($this->db_host,$this->db_user,$this->db_password) or die (mysql_error("ERROR: Could Not connect to server!"));
	  
	  mysql_select_db($this->db_database,$connect) or die (mysql_error("ERROR: Could not connect to database!"));


	  }//End function




}//End class


#5 sharkbate24

sharkbate24

    Private Pilot - IFR

  • Members
  • PipPipPipPip
  • 943 posts

Posted 17 July 2009 - 07:29 AM

Hi,

I'm not too sure, but try removing the parameters in mysql_error.

I believe MySQL error should be left like:

mysql_error();

Although I'm not too sure. It returns the error, so you don't fill it in.

EDIT: I noticed the variable "db_password" was spelt incorrectly while being declared. This could be why.

Thanks.

Edited by sharkbate24, 17 July 2009 - 07:32 AM.


#6 /home/fsw_member

/home/fsw_member

    Private Pilot - VFR

  • Members
  • PipPipPip
  • 418 posts
  • Location:/earth/

Posted 17 July 2009 - 09:33 AM

Hi,

Thanks for the reply, yeah typo with db_password. Its correct on the file just mis-type on here.  

The error is a parsing error and not a connection error: the mysql_errors are fine and correct.

the error actually starts with 'var $db_host = "";'

I could maybe try 'var = $db_host = "";' or 'var = $db_host[""];'

hmmm.

Thanks anyway.