Page 1 of 2
Objected Oriented or not!! can someone help
Posted: Wed Jul 05, 2006 10:23 am
by mohson
I know this will probably end up being a stupid question but I need advice.
Ive built an online contact management system for my project. I am currently writing my academic documentation.
Now I am writing my methodology and Im stuck, If I had developed this project in asp.net then I would have said I am programming the system in Object Oriented form BUT I programmed it using html,mySQL and PHP So what form have I programmed it in?
What is the opposite to OO or what type of programming in this context is PHP devlopment
Go on put me out of my misery
Posted: Wed Jul 05, 2006 10:28 am
by feyd
The opposite of object oriented is procedural. Procedural is typified as using few, if any, classes and objects. At some level there is a small amount of procedural in an object oriented php application as there is start up code that must be run to get into the objects. However, if the bulk of the application logic is found in classes, you are more on the object oriented side than procedural. If you have few functions and few classes you're pushing way deep into procedural, and non-modular development.
Posted: Wed Jul 05, 2006 10:51 am
by mohson
Ok thank you for that response, my system has basic functions, add a record, delete a record, edit records, search records using different criteria and download records. would each of these tasks be classed as a 'class' [pardon the pun] if so tha means I have a number of classes therefore object oriented.
BUT I was informed that php4 doesnt support pbject oriented programming [ whatever this person meant] he wasnt the only one to say it so did someone else.
BUT feyd the way you describe object oriented below indicates that it doesnt matter what version of php you code in, its how you code and what you code rather than what you use to code.
Maybe im making this more confusing than it is.
Anymore insight?
Posted: Wed Jul 05, 2006 11:35 am
by feyd
I'll try to put it in simple terms: if you have a lot more
Code: Select all
class foo
{
function fooMethod()
{
// do stuff
}
}
and less
Code: Select all
function fooFunction()
{
// do stuff
}
// do some more stuff
then you are on the object oriented side.
It is fairly true that PHP 4 isn't object oriented, nor is PHP 5, technically.. but that doesn't matter. They both support objects, therefore one can write object oriented code. You correctly understood that it is not what version you write the code in, but what you write.
Posted: Wed Jul 05, 2006 12:22 pm
by jason
Procedural code employing a
Page Controller pattern is what it sounds like to me. A fancy way of saying a simple web page. =)
Posted: Wed Jul 05, 2006 12:40 pm
by Christopher
I think saying that procedural is the opposite of object oriented is serious misstatement. They are different ways to process data that are more of a continuum than anything. In simplest terms an class/object provides a namespace and local vars for a set of functions. That is hardly opposing and is a natural step in building modular software. And it is my experience that programmers naturally switch between passing data to functions and calling methods on data. The differences in many ways are trivial. And much as programmers may spout, the design issues are the same.
I also think the object model in PHP4 is more than sufficient to program in an OO fashion. I would note that even in languages where everything is an object you will still see many code sequences where -- though the syntax is object syntax -- the code is procedural. PHP has has the class construct for a very long time. Not using a language construct when appropriate because of non-technical reasons is folly.
Posted: Wed Jul 05, 2006 12:54 pm
by RobertGonzalez
Really quick rundown...
OOP: Uses a series of functions (called methods) and variables (called properties) wrapped in a Class statement (called an object). When needed, you can initialize the class with a call to new ClassName in this manner...
In this example, the var $mail is actually an object that now has access to all the methods and properties of the Mailer class. You can use those methods and properties throught your script by using the $mail object along with the path name separator (->). So a method called send in the Mailer class could be accessed like this...
OK, that is as far as this intro goes on OOP.
Procedural: Proceudral style coding takes the essence of the classes, removes them from classes and essentially applies all of the functions to your entire script. This is most commonly achieved by the use of numerous functions in a page that is included by all pages of the script. Using procedural style coding is a common methodology employed by new PHP programmers (and some seasoned ones).
There has been an enormous, on-going debate over the merits of one versus the other. This is really not the place for that discussion. To answer your question, when you are not utilizing Object Oriented Programming technique in PHP, you are utilizing the commonly accepted term'procedural' coding style.
Posted: Wed Jul 05, 2006 1:57 pm
by Christopher
Not arguing merits of using any lanugage constructs, but I would point out that it is not a clear cut as the pundits say.
This is essentially procedural code even though it uses the class construct:
Code: Select all
$email new Email();
$email->send('caffinated@example.com', 'My Subject', $message);
Whereas this is essentially OO code, even though it does not use the class construct:
Code: Select all
$rh = fopen($file_source, 'rb');
$wh = fopen($file_target, 'wb');
while (!feof($rh)) {
if (fwrite($wh, fread($rh, 1024)) === FALSE) {
// 'Download error: Cannot write to file ('.$file_target.')';
return true;
}
}
fclose($rh);
fclose($wh);
This distinctions between paradigms is really a
design distinction and the "enormous, on-going debate" is much more about programmers stubbornly defending their poor coding habits than actually discussing the many interesting
Programming Paradigms that can be used. Those paradigms are, in fact, regularly mixed together by good programmers every time they code.
In answer to the OP, you are perhaps not really asking about
Methodology as you say. If you were then the choice would probably be between Structured Programming and Object-Oriented Programming in PHP -- but as I have said above -- the lines are very blurred as to where one ends and the other begins in todays programming -- more overlap than difference. And even then, Methodology is really still about
practices and not language construct choices.
Posted: Wed Jul 05, 2006 5:44 pm
by mohson
WOW - This is the word that comes to mind ive never wriiten a post which has received this kind of response. A lot of different responses and a little bit og jargon which completely flew by me. Thanks for all the input.
Let me focus the question a little. when using something like ASP the .NET framework your coding is called oo programming because you create objects on your page amd then code them. This is not so for PHP coding hence why it is called as you say procedural programming.
But with procedural programming do you still use classes or are classes only required when a system is more complex. Example is ADD RECORDS a class and SEARCH RECORDS a different class.
Looking at my system above how ould you describe the way i which I am coding? procedural or OOP?
Also has anyone here heard of sequence diagrams, if YES then can these still be used from procrdural programming?
Any adivce and guidance appreciated
Posted: Wed Jul 05, 2006 5:50 pm
by feyd
whether "add records" and "search records" are classes in your code would depend on how you wrote them. To me, they would be methods of a database class. Until we see your code, we can't say which form you are writing in.. Simple as that.
Posted: Wed Jul 05, 2006 6:34 pm
by Christopher
mohson wrote:Let me focus the question a little. when using something like ASP the .NET framework your coding is called oo programming because you create objects on your page amd then code them. This is not so for PHP coding hence why it is called as you say procedural programming.
I am not sure where you got this idea, but you can code pages in ASP (you don't say whether it is VB or C#) and PHP using the same design and by implementing similar code. I think you are really mixing up methodologies, paradigms and practices here. And just because you are creating "objects" does not necessarily mean your code/design is OO (or for that matter any good).
mohson wrote:But with procedural programming do you still use classes or are classes only required when a system is more complex. Example is ADD RECORDS a class and SEARCH RECORDS a different class.
Again you seem to think that programming in PHP for some reason has a different "name" than programming in "ASP". Not sure where you got that idea, but you can program in different styles in both ASP and PHP. The class construct is certainly something created with OO in mind, but that does not mean just by typing "class" you are using OO.
mohson wrote:Looking at my system above how ould you describe the way i which I am coding? procedural or OOP?
Since you have not posted any code I really don't know. But I really think you are missing the point here. The question is not how to describe your programming. The question is how good are your designs and code?
Posted: Thu Jul 06, 2006 9:26 am
by mohson
Ok here is why I displayall records this is one example of my code.
Code: Select all
<?php include "dyn.header"; ?>
<!-- Please don't remove these comments -->
<!-- Content Start -->
<p>
<strong><font size="4">Personal Details</font></strong><br>
<br>
<p><font><strong>Select a record for editing by clicking on its PID.</strong></font>
<br><a href="queries.html"> Quick Search: Find a Contact</a><br>
<hr>
<?php
// config-------------------------------------
$host = "xxxxx"; //your database host
$user = "xxxx"; // your database user name
$pass = "xxxxx"; // your database password
$db = "contact_management_system"; // your database name
$filename = "people.html"; // name of this file
$option = array (5, 10, 20, 50, 100, 200);
$default = 100; // default number of records per page
$action = $_SERVER['PHP_SELF']; // if this doesn't work, enter the filename
// database query. Enter your query here
$query = "SELECT
o.org_id,o.web_url,
p.person_id,p.org_id,p.salutation,p.firstname,p.surname,
p.organisation,p.role,p.address1,p.address2,p.city,
p.postcode,p.telephone,p.mobile,p.fax,p.dateoflastcontact,
p.datecontactagain,p.email,p.consultation_panel_member,
p.primary_contact,p.primarycontactemail,
p.advertising_grad_jobs,p.offer_mscproject,p.offer_ugproject,
p.professional_devactivities,p.bcs_membership,p.bcs_pds,
p.teaching_courses,p.academic_consultancy,
DATE_FORMAT(dateoflastcontact, '%M/%Y')
AS dateoflastcontact, DATE_FORMAT(datecontactagain, '%M/%Y')
AS datecontactagain
FROM people p LEFT JOIN organisations o
ON o.org_id = p.org_id
ORDER BY organisation";
// end config---------------------------------
$opt_cnt = count ($option);
$go = $_GET['go'];
// paranoid
if ($go == "") {
$go = $default;
}
elseif (!in_array ($go, $option)) {
$go = $default;
}
elseif (!is_numeric ($go)) {
$go = $default;
}
$nol = $go;
$limit = "0, $nol";
$count = 1;
echo "<form name=\"form1\" id=\"form1\" method=\"get\" action=\"$action\">\r\n";
echo "<select name=\"go\" id=\"go\">\r\n";
for ($i = 0; $i <= $opt_cnt; $i ++) {
if ($option[$i] == $go) {
echo "<option value=\"".$option[$i]."\" selected=\"selected\">".$option[$i]."</option>\r\n";
} else {
echo "<option value=\"".$option[$i]."\">".$option[$i]."</option>\r\n";
}
}
echo "</select>\r\n";
echo "<input type=\"submit\" name=\"Submit\" id=\"Submit\" value=\"Go\" />\r\n";
echo "</form>\r\n";
$connection = mysql_connect ($host, $user, $pass) or die ("Unable to connect");
mysql_select_db ($db) or die ("Unable to select database $db");
// control query------------------------------
/* this query checks how many records you have in your table.
I created this query so we could be able to check if user is
trying to append number larger than the number of records
to the query string.*/
$off_sql = mysql_query ("$query") or die ("Error in query: $off_sql".mysql_error());
$off_pag = ceil (mysql_num_rows($off_sql) / $nol);
//--------------------------------------------
$off = $_GET['offset'];
//paranoid
if (get_magic_quotes_gpc() == 0) {
$off = addslashes ($off);
}
if (!is_numeric ($off)) {
$off = 1;
}
// this checks if user is trying to put something stupid in query string
if ($off > $off_pag) {
$off = 1;
}
if ($off == "1") {
$limit = "0, $nol";
}
elseif ($off <> "") {
for ($i = 0; $i <= ($off - 1) * $nol; $i ++) {
$limit = "$i, $nol";
$count = $i + 1;
}
}
// Query to extract records from database.
$sql = mysql_query ("$query LIMIT $limit") or die ("Error in query: $sql".mysql_error());
// Define your colors for the alternating rows
$color1 = "#ADD8E6";$color2 = "#E0FFFF";
$color = $color2;echo
"<table width=\"50%\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">
<tr>
<td><b><small>RecNo</small></b></td>
<td><b><small></small></b></td>
<td><b><small>Title</small></b></td>
<td><a href=\"orderfirstname.html\"><b><small>First Name</small></b></a></td>
<td><a href=\"ordersurname.html\"><b><small>Surname</small></b></a></td>
<td><a href=\"orderorgdesc.html\"><b><small>Organisation</small></b></a></td>
<td><b><center><small>Role</small></center></b></td>
<td><b><small>Address(1)</small></b></td>
<td><b><small>Address(2)</small></b></td>
<td><b><small>City</small></b></td>
<td><b><small>Post Code</small></b></td>
<td><b><small>Telephone</small></b></td>
<td><b><small>Mobile</small></b></td>
<td><b><small>Fax</small></b></td>
<td><b><small>Last Contact</small></b></td>
<td><b><small>Contact Again</small></b></td>
<td><b><small>Consultation Panel</small></b></td>
<td><b><small>Primary Contact</small></b></td>
<td><b><small>Graduate Jobs</small></b></td>
<td><b><small>MSC Projects</small></b></td>
<td><b><small>UG Projects</small></b></td>
<td><b><small>Professional Dev</small></b></td>
<td><b><small>BCS Mem.</small></b></td>
<td><b><small>BCS PDS</small></b></td>
<td><b><small>Teaching</small></b></td>
<td><b><small>Academic Consultancy</small></b></td>";
while ($row = mysql_fetch_object($sql))
{($color==$color2)? $color = $color1 : $color = $color2;
echo "<tr bgcolor=\"$color\"><td>".$count . '</td>
<td>'.'<a href="editpeople.html?person_id='.$row->person_id. '">edit</a></td>
<td>'.$row->salutation .'</td>
<td>'.'<a href=mailto:'.$row->email.'>'.$row->firstname .'</a></td>
<td>'.'<a href=mailto:'.$row->email.'>'.$row->surname .'</a></td>
<td>'.'<a href=http://'.$row->web_url.'>'.$row->organisation . '</a></td>
<td>'.$row->role.'</td>
<td>'.$row->address1 .'</td>
<td>'.$row->address2 .'</td>
<td>'.$row->city .'</td>
<td>'.$row->postcode .'</td>
<td>'.$row->telephone .'</td>
<td>'.$row->mobile .'</td>
<td>'.$row->fax .'</td>
<td>'.$row->dateoflastcontact.'</td>
<td>'.$row->datecontactagain.'</td>
<td>'.$row->consultation_panel_member.'</td>
<td>'.'<a href=mailto:'.$row->primarycontactemail.'>'.$row->primary_contact.'</td>
<td>'.$row->advertising_grad_jobs.'</td>
<td>'.$row->offer_mscproject .'</td>
<td>'.$row->offer_ugproject.'</td>
<td>'.$row->professional_devactivitites .'</td>
<td>'.$row->bcs_membership .'</td>
<td>'.$row->bcs_pds .'</td>
<td>'.$row->teaching_courses .'</td>
<td>'.$row->academic_consultancy .'<td>';
$count += 1;
}
echo "</table>";
echo "<br /><br />\r\n";
if ($off <> 1) {
$prev = $off - 1;
echo "[ < <a href=\"$filename?offset=$prev&go=$go\">prev</a> ] \r\n";
}
for ($i = 1; $i <= $off_pag; $i ++) {
if ($i == $off) {
echo "[<b> $i </b>] \r\n";
} else {
echo "[ <a href=\"$filename?offset=$i&go=$go\">$i</a> ] \r\n";
}
}
if ($off < $off_pag) {
$next = $off + 1;
echo "[ <a href=\"$filename?offset=$next&go=$go\">next</a> > ] \r\n";
}
echo "<br /><br />\r\n";
echo "Page $off of $off_pag<br />\r\n";
?>
<a href=http://editweb.soi.city.ac.uk/organisation/pl/CMS/export_csv.php> Dowload<a/>
<p>
<!-- Content End -->
<!-- Please don't remove these comments -->
<?php include "dyn.footer"; ?>
Posted: Thu Jul 06, 2006 9:29 am
by mohson
Heres where I add records another example of my code
Code: Select all
<?php include "dyn.header"; ?>
<!-- Please don't remove these comments -->
<!-- Content Start -->
<h1>Administration</h1>
<?php
/*** Function: DateSelector**
Input: STRING inName, INTEGER useDate** Output: **
Description: Creates three form fields for get month/day/year*/ function
DateSelector($inName, $useDate)
{ $monthName = array(1=>"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December");
$style = "style=\"color: #000000; background-color: #ADD8E6\">";
if($useDate == "")
{
$useDate = time();
}
print("<SELECT NAME=\"" . $inName . "Month\" $style >\n");
for($currentMonth = 1; $currentMonth <= 12; $currentMonth++)
{
echo "<OPTION VALUE=\"";
echo intval($currentMonth);
echo "\"";
if(intval(date("m", $useDate))==$currentMonth)
{
echo " SELECTED";
}
echo ">".$monthName[$currentMonth]."\n";
}
echo "</SELECT>";
echo "<SELECT NAME=".$inName."Day $style >\n";
for($currentDay=1; $currentDay <= 31; $currentDay++)
{
echo "<OPTION VALUE=\"$currentDay\"";
if(intval(date("d", $useDate))==$currentDay)
{
echo " SELECTED";
}
echo ">$currentDay\n";
}
echo "</SELECT>";
echo "<SELECT NAME=".$inName."Year $style>\n";
$startYear = date("Y", $useDate);
if($startYear < 1997)
{
$startYear = date("Y");
}
for($currentYear = $startYear-1; $currentYear <= $startYear+2;$currentYear++) {
echo "<OPTION VALUE=\"$currentYear\"";
if(date("Y", $useDate)==$currentYear)
{
echo " SELECTED";
}
echo ">$currentYear\n";
} echo "</SELECT>";}
?>
<?php
/* Connecting, selecting database */
$link = mysql_connect("xxxxxxxxx", "xxxx", "xxxx")
or die("Could not connect : " . mysql_error());
echo "";
mysql_select_db("contact_management_system") or die("Could not select database");
?>
<body onLoad="focus()">
<form method="post" action="processpeople.html">
<table width="100%" border="0">
<tr>
<td width="17%"><strong>Salutation</strong></td>
<td width="27%">
<select name="salutation" style="color: #000000;
background-color: #ADD8E6">
<option>Mr</option>
<option>Mrs</option>
<option>Ms</option>
<option>Miss</option>
<option>Prof</option>
<option>Dr</option>
</select>
</td>
<td width="27%"><strong>Telephone</strong></td>
<td width="29%"><input name="telephone" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20"></td>
</tr>
<tr>
<td><strong>First Name</strong></td>
<td>
<input name="firstname" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</td>
<td><strong>Mobile</strong></td>
<td><input name="mobile" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20"></td>
</tr>
<tr>
<td><strong>Surname</strong></td>
<td>
<input name="surname" type="text" size="15"style="color: #000000;
background-color: #ADD8E6">
</td>
<td><strong>Fax</strong></td>
<td>
<input name="fax" type="text"style="color: #000000;
background-color: #ADD8E6" size="20" maxlength="20">
</td>
</tr>
<tr>
<td><strong>Organisation</strong></td>
<td><input name="organisation" type="text" size="20" maxlength="100" style="color: #000000; background-color: #ADD8E6"></td>
<td><strong>E-mail</strong></td>
<td>
<input name="email" type="text"style="color: #000000;
background-color: #ADD8E6" size="25" maxlength="50">
</td>
</tr>
<tr>
<td><strong>Role</strong></font></td>
<td><input name="role" type="text" size="25"style="color: #000000;
background-color: #ADD8E6">
</td>
<td><strong>Date of Last Contact</strong></td>
<td>
<?php DateSelector("dateoflastcontact_",""); ?>
</font></td>
</tr>
<tr>
<td><strong>Address (1)</strong></td>
<td>
<input name="address1" type="text" size="20" style="color: #000000;
background-color: #ADD8E6">
</td>
<td><strong>Date Contact Again</strong></td>
<td>
<?php DateSelector("datecontactagain_",""); ?>
</td>
</tr>
<tr>
<td><strong>Address(2) </strong></td>
<td> <input name="address2" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</td>
<td><strong>OID</strong></td>
<td>
<input name="org_id" type="text"style="color: #000000;
background-color: #ADD8E6" size="5">
</td>
</tr>
<td><strong>City</strong></font></td>
<td>
<input name="city" type="text" size="20"style="color: #000000;
background-color: #ADD8E6">
</td>
</tr>
<tr>
<td><strong>Post Code</strong></td>
<td><input name="postcode" type="text" size="7"style="color: #000000;
background-color: #ADD8E6">
</td>
<tr>
<td width="17%"><strong>Industry</strong></td>
<td width="27%">
<select name="industry" style="color: #000000;
background-color: #ADD8E6">
<option></option>
<option>Games Technology</option>
<option>Technical Support</option>
<option>Programmer</option>
<option>Networking</option>
<option>Web</option>
<option>Analyst</option>
</select>
</td>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td><b>************</b></td>
<td><b>************</b></td>
<tr><td><small><b>Consultation Panel Register<b><small></td>
<tr><td><b>Consultation Panel</b></td>
<td>
<select name="consultation_panel_member" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
<tr>
<td><b>Primary Contact</b></td>
<td>
<input name="primary_contact" type="text" size="20"style="color: #000000;
background-color: #E0FFFF">
</td>
</tr>
<tr>
<td><b>Primary Contact E-mail</b></td>
<td>
<input name="primarycontactemail" type="text" size="25"style="color: #000000; background-color: #E0FFFF">
</td>
</tr>
</td>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td><b>************</b></td>
<td><b>************</b></td>
<tr><td><small><b>Employer Feedback.......<b><small></td>
</tr>
<tr>
<td><b>Employer Feedback?</b></td>
<td>
<select name="employer_feedback" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td><b>************</b></td>
<td><b>************</b></td>
<tr><td><small><b>Interested in discussing.......<b><small></td>
</tr>
<tr>
<td><b>Advertising Graduate Jobs</b></td>
<td>
<select name="advertising_grad_jobs" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
<tr><td><b>Offer MSC Project Work</b></td>
<td>
<select name="offer_mscproject" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
<tr><td><b>Offer Undergraduate Projects</b></td>
<td>
<select name="offer_ugproject" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
<tr><td><b>Professional Developement Activities</b></td>
<td>
<select name="professional_devactivities" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
<tr><td><b>Membership of BCS</b></td>
<td>
<select name="bcs_membership" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
<tr><td><b>Professional Developement</b></td>
<td>
<select name="bcs_pds" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
<tr><td><b>Teaching Courses</b></td>
<td>
<select name="teaching_courses" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
<tr><td><b>Academic Consultancy</b></td>
<td>
<select name="academic_consultancy" style="color: #000000;
background-color: #E0FFFF">
<option></option>
<option>yes</option>
</select>
</td>
</tr>
</table>
<input type="submit" name="submit" value = "Enter Information">
</form>
<p>
<!-- Content End -->
<!-- Please don't remove these comments -->
<?php include "dyn.footer"; ?>
Posted: Thu Jul 06, 2006 9:29 am
by feyd
The code is not only procedural to me, but also somewhat difficult to read due to your formatting choices.
Posted: Thu Jul 06, 2006 10:19 am
by RobertGonzalez
Feyd++. The only OOP in there is the $row var being set to mysql_fetch_object. Other than that, it is all procedural.