...

View Full Version : OOP/Class Question



Fatman
03-20-2007, 11:04 AM
Hi all,

I am an fairly experienced PHP programmer - but I am used to procedural code - Currently I am refactoring to OOP.

My question is as follows:

I have 2 classes - a progressbar class and data fetcher class.

In this implementation, the data fetcher need to directly access the progressbar.

I could use extends, but then i cant use the datafetcher and progressbar classes independantly.

What is the best way for me to achieve this?

Thanks
Fatman

ess
03-20-2007, 12:59 PM
If classes have a direct relationship, than inheritance should be used in this case.

In other words, you should check what is referred to as "is a", or "has a" relationship. The "is a" relationship tells us that a class should inherit from a parent class, where as the "has a" relationship tells us that a class is composed of another class.

For example, say that we have two classes. Our first class is called "House" and our second class is called "Door". to establish the relationship between those classes, you ask 2 questions.
1- IS A house a door?
2- Does a house HAS A door?

You would agree that the second question is applicable in this case, and therefore, we apply composition (a class is composed of another class). However, if we examine the relationship between a "Person" and an "Employee", than we will find that an "Employee" IS A "Person" and in this case, the Employee class should inherit from the Person class.

I hope that answers your question. I wrote a quick answer as I am having my lunch break and haven't got much time.

Cheers,
Ess

aedrin
03-20-2007, 04:13 PM
My assumption would be:

A DataFetcher uses a ProgressBar, but it is not an evolution of ProgressBar. Hence they are seperate classes that do not inherit.

The best way is to have a "progressBar" variable inside of the DataFetcher class, assign it in the constructor and then use it with $this->progressBar.



class ProgressBar {
public $value;
}

class DataFetcher {
private $progressBar;

function __construct() {
$this->progressBar = new ProgressBar();
$this->progressBar->value = 0;
}
}

firepages
03-21-2007, 01:13 AM
aedrin +1

With an Interpreted language like PHP its important to keep inheritance structures as lean as possible (they are all loaded at runtime!), JAVA like inheritance is fine for a compiled (nearly) language like JAVA but has performance issues in PHP, so only use inheritance if there is a compelling need or a very close relationship.

e.g. yes a house has a door, but so does a plane, submarine tree house etc so a door is probably best aggregated as per aedrin's example.

e.g+ cars also have doors, so does your door class also subclass car ?, without multiple inheritance, (which php does not possess) it can't, so we are into the realm of duplicated code.

I can however imagine a 'door' superclass that had subclasses for different types of door.



<?php
class car{
function car(){
$this->door=new door();//or new car_door which extends door!
}
}

class house{
function house(){
$this->door=new door('house');//else pass some data for lazy loading within door
}
}
?>

etc

Fatman
04-10-2007, 02:24 PM
Thansk for the replies

I got it now :


class ProgressBar {
public $value;
}

class DataFetcher {
private $progressBar;

function __construct() {
$this->progressBar = new ProgressBar();
$this->progressBar->value = 0;
}
}


How do i access progressBars methods?

$DataFetcher->progressBar->set(X) ??

aedrin
04-10-2007, 02:36 PM
That would only work if you make it public instead of private.

Or you could add a method called getProgressBar() which returns the instance of the progress bar that it contains.

Fatman
04-10-2007, 03:08 PM
well im on php4 which i beleive doesnt support private public declarations

I replaced the public in the example with a "var"

aedrin
04-10-2007, 03:12 PM
well im on php4 which i beleive doesnt support private public declarations

I'd recommend switching to PHP5. You're just hurting yourself (and us ;)) in the process.

GJay
04-10-2007, 05:25 PM
if things other than the DataFetcher need to be able to talk to the ProgressBar, then it maybe shouldn't be structured as you have it. If instead, they just need to tell the DataFetcher something, then let them do that, and let the DataFetcher worry about passing the required bits on to the ProgressBar.

Fatman
04-16-2007, 10:15 AM
Ok well refernce the variable (as a private) works from the constructor using the code below:

$this->prb->moveStep(Blah)

But from a class member function, i get :

Fatal error: Call to a member function on a non-object

What am i doing wrong?

aedrin
04-16-2007, 03:38 PM
Somewhere the variable $this->prb is getting modified to not be a class/object, or the progressBar somehow got rid of itself.

It depends on your code.

Fatman
04-16-2007, 10:01 PM
Somewhere the variable $this->prb is getting modified to not be a class/object, or the progressBar somehow got rid of itself.

It depends on your code.

Ok here is the isolated code:

testfile.php


<?
include("testclass.php");

$test = new testing();

$test->movebar(40);
?>


Class:


<?

class testing{

var $prbar;

function __construct() {

$this->time=time();
$this->prbar = new ProgressBar(300, 20);

}

function movebar($distance){

$this->prbar->moveStep($distance);

}

}
?>
Still getting:

Fatal error: Call to a member function on a non-object in /home/fatman/public_html/testclass.php on line 15

GJay
04-16-2007, 10:55 PM
if you're using PHP4 then __construct won't be called. Constructors before 5 take the name of the class, so


function testing() {
$this->prbar = new Progressbar();
}

Fatman
04-17-2007, 09:46 AM
Ah Thanks!

I checked out your blog too GJay



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum