Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP class design, how to add the same functionality to different and unrelated classes

Posted on 2003-10-25
6
192 Views
Last Modified: 2007-12-19
Dear all,

I have a design problem to submit to you guys.

Here it goes...

A class represents a series of pictures, one picture per object.

class Picture {
  var $pictureName;
  }

Another class represents a series of books, one book per object.

class Book {
  var $bookName;
  }

Now I would like to have for each of these two classes, a member function to sort these objects. The sorting is done using an arbitrary list order.

So I first modified the picture class, adding a member variable and the corresponding member functions.

class Picture {
  var $pictureName;
  var $listOrder;

  function sortByListOrder() {};
  function setListOrder() {};
  function getListOrder() {};
  }

I does the job. Fine.

To get the same functionality to the book class, I copied and pasted the code into the book class.

It works, but I have duplicated code in now two classes. And more classes are lined up craving for this sorting functionality.

I'm therefore looking for a way to add this
  var $listOrder;

  function sortByListOrder() {};
  function setListOrder() {};
  function getListOrder() {};
to a class without doing any copy/paste.

Any design suggestion..?

Cheers
Stephane



0
Comment
Question by:stephaneeybert
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:aolXFT
ID: 9619597
<?php

This is a job for inhereitance

class sortingclass{
  function sortByName(){

  }

  function sortByOtherCriteria(){

  }
}

class
0
 

Author Comment

by:stephaneeybert
ID: 9620863
But my class is already used in many pages. I can't use a derived class name in all those pages.
And the class already inherits from a base class.

What about multiple inheritance..?
0
 
LVL 11

Expert Comment

by:shmert
ID: 9625034
It's not clear to me what the sortByListOrder() method does, since a Picture object only contains a single picture.  Or does it?  You mentioned that
    > A class represents a series of pictures, one picture per object.

At any rate, for sorting functionality I'd recommend getting rid of the sortByListOrder() method and implementing an external comparator function and using usort().

class Picture {
  var $pictureName;
  var $listOrder;

  function setListOrder($newOrder) { $this->listOrder = $newOrder; }
  function getListOrder() { return $this->listOrder(); }
}

function compareObjects($someObject, $otherObject) {
    if ($someObject->listOrder == $otherObject->listOrder) return 0;
    return ($someObject->listOrder < $otherObject->listOrder) ? -1 : 1;
}

$firstPicture = new Picture('theFirstPicture.jpg');
$firstPicture->setSortOrder(1);
$secondPicture = new Picture('theSecondPicture.jpg');
$secondPicture->setSortOrder(2);
$unsorted = array($secondPicture, $firstPicture);
$sorted = usort($unsorted, 'compareObjects')
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:stephaneeybert
ID: 9625806
Hello shmert!

Thanks for your comment.

The sorting aspect here is not so relevant. The functionality could be anything. The problem is how to use, in different classes!, a  set of member variables and corresponding member functions.

In my case the member variable is
  var $listOrder;

and the member functions are
  function setListOrder($newOrder) { $this->listOrder = $newOrder; }
  function getListOrder() { return $this->listOrder(); }
  function sortByListOrder() { return $this->listOrder(); }

This variable and these functions are used in several different classes.

I copied and pasted the source code.

Not the smartest way to go...

So I'm looking for a design that would answer this problem.

Cheers

Stephane

0
 
LVL 11

Expert Comment

by:shmert
ID: 9627499
Well, I think aolxft had the right idea, but didn't go into depth explaining it.  Create a common super-class called 'Sortable' and have both the Picture class and the Book class extend from this sortingClass.

class Sortable {
  var $listOrder;
  function setListOrder($newOrder) { $this->listOrder = $newOrder; }
  function getListOrder() { return $this->listOrder(); }
  function sortByListOrder() { return $this->listOrder(); }
}

class Picture extends Sortable {
  // picture stuff here
}

class Book extends Sortable {
  // book stuff here
}
0
 
LVL 11

Accepted Solution

by:
shmert earned 50 total points
ID: 9627532
If this isn't an option (for instance, if you already have a super-class that you can't change for some reason), you could use encapsulation over inheritance.  This means you would create a sortable class similar to above, but instead of extending it, the Book and Picture classes would have a variable that holds a Sortable object.  All the logic for sorting is contained in the "Sortable" class, which is reused in as many classes as you like.  Any class that wants to use the sortable functionality just has a variable to store the sortable object.

class Sortable {
  var $listOrder;
  function setListOrder($newOrder) { $this->listOrder = $newOrder; }
  function getListOrder() { return $this->listOrder(); }
  function sortByListOrder() { return $this->listOrder(); }
}

class Picture {
  var $sortable;
  function setSortable($sortable) { $this->sortable = $sortable; }
  // picture stuff here
}

$picture = new picture ('image.jpg');
$sortable = new Sortable();
$sortable->setListOrder(123);
$picture->setSortable($sortable);
$picture->sortable->sortByListOrder();
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question