Solved

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

Posted on 2003-10-25
6
197 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
[X]
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
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

728 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