Solved

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

Posted on 2003-10-25
6
189 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scandir() not reads all files 4 27
how to resize preview image 4 33
wordpress issue 2 24
Help cleaning out CSS 2 32
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now