Opinions on how OOP should be used please
Posted: Wed Apr 14, 2010 2:11 pm
Hi all,
I am starting to look into PHP OOP and I feel somewhat confused on how to use it. and i was hoping i could get some opinions on it here.
So here is what i do know... you will have a file containing your classes and your methods. you will have your normal php pages. You will create a "link" back to the class file on your normal php file and then reference back to the class file in order to get some code. Now i have seen code being printed out on the normal page that comes from the class file. but in the class file the code would contain html layout. I was told that is not how you should do it. i know OOP is for re-using code, so you only have to change your code 1 place and not in 14 different php files. but it should not be used to spit out data and the html layout. It should return variables etc but not e.g a html table and having a PHP while loop in the class file.
so just to show an example in my class.php i have following:
class skillMatrix {
and in my page.php i have:
Is this the correct way? that you create your code in class.php, take out the data in page.php and then use it ? now ofcourse now i have the second part on all my pages and now i still have to change alot of code.
Hope someone can help me clarify...
Rippie
I am starting to look into PHP OOP and I feel somewhat confused on how to use it. and i was hoping i could get some opinions on it here.
So here is what i do know... you will have a file containing your classes and your methods. you will have your normal php pages. You will create a "link" back to the class file on your normal php file and then reference back to the class file in order to get some code. Now i have seen code being printed out on the normal page that comes from the class file. but in the class file the code would contain html layout. I was told that is not how you should do it. i know OOP is for re-using code, so you only have to change your code 1 place and not in 14 different php files. but it should not be used to spit out data and the html layout. It should return variables etc but not e.g a html table and having a PHP while loop in the class file.
so just to show an example in my class.php i have following:
class skillMatrix {
Code: Select all
public function createIntranetNavi() {
$links = array(
'Show Skills Matrix' => 'skillsmatrix/skillsmatrix.php',
'Edit Skills Matrix' => 'skillsmatrix/admin/index.php',
'Show Tech Resource' => 'techresource/techresource.php',
'Edit Tech Resource' => 'techresource/admin/index.php'
);
return $links;
}Code: Select all
require_once '../includes/class.lib.php'; // contain my object methods
$skillMatrix = new skillMatrix();
$links = $skillMatrix->createNavi();
<div id="navigation">
<ul class="navul">
<li class="navheader">Navigation</li>
<?php
foreach ($links as $key => $value) {
$lengthOfFileName = strlen($value);
$extractedName = substr($_SERVER['PHP_SELF'], -($lengthOfFileName));
if ($extractedName == $value) {
echo "<li class=\"navlink\"><strong>$key</strong></li>";
} else {
echo "<li class=\"navlink\"><a class=\"menu\" href=\"$value\">$key</a></li>";
}
}
?>
<li class="navfooter"></li>
</ul>
</div>
Hope someone can help me clarify...
Rippie