Opinions on how OOP should be used please

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Opinions on how OOP should be used please

Post by Rippie »

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 {

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;
    }
and in my page.php i have:

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>
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
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: Opinions on how OOP should be used please

Post by Rippie »

Just an addition.... is it wrong to use OOP to spit out header navigation and footer part of your normal pages ? so the code in your class.php would indead contain some HTML layout ?

Rippie
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Opinions on how OOP should be used please

Post by social_experiment »

I created something similar but placed it inside a method in my class and called it on each page :

Code: Select all

<?php
protected function admin_navigation() {
         foreach ($this->links as $key => $value) {
                $lengthOfFileName = strlen($value);
                $extractedName = substr($_SERVER['PHP_SELF'], -($lengthOfFileName));                
                if ($extractedName == $value) {
                    echo "<span class=\"inactive\">$key</span>";
                }
                else {
                    echo "<a href=\"$value\" title=\"$key\">$key</a>";
                }
        }
     } ?>
 
And then on each page i would have :

Code: Select all

<?php
 $ObjectInstance = new Class_Name;
 $ObjectInstance->admin_navigation();
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: Opinions on how OOP should be used please

Post by Rippie »

social_experiment wrote: And then on each page i would have :

Code: Select all

<?php
 $ObjectInstance = new Class_Name;
 $ObjectInstance->admin_navigation();
?>
So you would place $ObjectInstance->admin_navigation(); where ever you needed it on your pages right ?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Opinions on how OOP should be used please

Post by social_experiment »

Yes, each time i need a navigation bar i use the method.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Rippie
Forum Commoner
Posts: 76
Joined: Sun Jan 10, 2010 11:32 am
Location: Nottingham

Re: Opinions on how OOP should be used please

Post by Rippie »

what leads me to this post is that some things we will always use...

HEADER
NAVIGATION
FOOTER

So if we could get away from the main pages and call them from somewhere that would be sweet :) Than then raises the question, should we leave the DOC type on each page or put that into HEADER ? but then we need to consider for page title..
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Opinions on how OOP should be used please

Post by social_experiment »

Rippie wrote:should we leave the DOC type on each page or put that into HEADER ? but then we need to consider for page title..
I use a normal HTML doc type header and then place the php around it, where it needs to be.

Code: Select all

<?php
 require_once 'file.php';
  //DOCTYPE here
 
 /*
 other code
 */
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply