For your question about working with Content
and Content-v2
(which are horrible names, btw)...
- Go read more about encapsulation and polymorphism
- I think you need a
BaseContent
class that is an abstract class that is never consumed in any way other than by inheriting.
- From your
BaseContent
class, you should then have an HtmlContent
, PdfContent
, MSWordContent
, etc. classes. And if you want, you can even extend these, such as with a HtmlReportContent
(extends HtmlContent
), MSWord2010Content, etc.
- Doing it this way, you can declare and store variables all of type BaseContent but when you instantiate them, you instantiate them as your specific type. When you do this, even if you have a Render() method implemented in your BaseContent class, your children classes can opt to use the base Render() method or override it and provide their own implementation, or even override it, provide a little custom implementation, and then call the base implementation to benefit from that implementation as well.
If you have shared implementation or behavior (for example, a Chihuahua
and a GermanShepard
class would both have a Bark()
function but they would be implemented differently), then abstract that shared implementation or behavior to a base class. You don't necessarily have to provide an implementation in your base class - that's why it's an abstract class - it then forces your child classes to implement it (Bark()
would be defined in your BaseDog
class but not implemented).
As for the flow of your system...
For your Object-Oriented design, think of designing a state machine and your objects are essentially filling out that state machine's purpose and every change from one state to another state. Yeah, there is probably one way to walk through your state-flow diagram that covers a good 50% of scenarios, but we all know that users will deviate from that. So you need to define the ways that a user can deviate from it and allow those but restrict to those. You may or may not actually draw out a flow diagram, depending on how complex your system is. But sometimes it helps to visualize some of the possibilities by drawing out part of it. Typically, though, this diagram would be way too large for most OO systems to actually create. If you were doing a system for NASA, you would probably have it, though. :-P
Here is some code to help illustrate some of these things (and to address some of your questions in the comments). However, I'm NOT a PHP guy so I'm going to give you C# examples but I'll keep them simple enough that you should be able to translate into PHP easily enough.
public interface IAnimal
{
string GetName();
string Talk();
}
public abstract class AnimalBase : IAnimal
{
private string _name;
// Constructor #1
protected AnimalBase(string name)
{
_name = name;
}
// Constructor #2
protected AnimalBase(string name, bool isCutsey)
{
if (isCutsey)
{
// Change "Fluffy" into "Fluffy-poo"
_name = name + "-poo";
}
}
// GetName implemention from IAnimal.
// In C#, "virtual" means "Let the child class override this if it wants to but is not required to"
public virtual string GetName()
{
return _name;
}
// Talk "implementation" from IAnimal.
// In C#, "abstract" means "require our child classes to override this and provide the implementation".
// Since our base class forces child classes to provide the implementation, this takes care of the IAnimal implementation requirement.
abstract public string Talk();
}
public class Dog : AnimalBase
{
// This constructor simply passes on the name parameter to the base class's constructor.
public Dog(string name)
: base(name)
{
}
// This constructor passes on both parameters to the base class's constructor.
public Dog(string name, bool isCutsey)
: base(name, isCutsey)
{
}
// Override the base class's Talk() function here, and this satisfy's AnimalBase's requirement to provide this implementation for IAnimal.
public override string Talk()
{
return "Woof! Woof!";
}
}
public class SmallDog : Dog
{
private bool _isPurseDog;
// This constructor is unique from all of the other constructors.
// Rather than the second boolean representing the "isCutsey" property, it's entirely different.
// It's entirely a coincidence that they're the same datatype - this is not important.
// Notice that we're saying ALL SmallDogs are cutsey by passing a hardcoded true into the base class's (Dog) second parameter of the constructor.
public SmallDog(string name, bool isPurseDog)
: base(name, true)
{
_isPurseDog = isPurseDog;
}
// This tells us if the dog fits in a purse.
public bool DoesThisDogFitInAPurse()
{
return _isPurseDog;
}
// Rather than using Dog's Talk() implementation, we're changing this because this special type of dog is different.
public override string Talk()
{
return "Yip! Yip!";
}
}
public class Chihuahua : SmallDog
{
private int _hatSize;
// We say that Chihuahua's always fit in a purse. Nothing else different about them, though.
public Chihuahua(string name, int hatSize)
: base(name, true)
{
_hatSize = hatSize;
}
// Of course all chihuahuas wear Mexican hats, so let's make sure we know its hat size!
public int GetHatSize()
{
return _hatSize;
}
}
public class Cat : AnimalBase
{
// This constructor simply passes on the name parameter to the base class's constructor.
public Cat(string name)
: base(name)
{
}
// This constructor passes on both parameters to the base class's constructor.
public Cat(string name, bool isCutsey)
: base(name, isCutsey)
{
}
// Override the base class's Talk() function here, and this satisfy's AnimalBase's requirement to provide this implementation for IAnimal.
public override string Talk()
{
return "Meoooowwww...";
}
}
public class Lion : Cat
{
public Lion(string name)
: base(name)
{
}
// Rather than using Cat's Talk() implementation, we're changing this because this special type of cat is different.
public override string Talk()
{
return "ROAR!!!!!!!!";
}
}
public class ThisIsNotAGoodExampleOfObjectOrientedCoding
{
public string DoStuff()
{
// To keep the C#-to-PHP translation easy, think of this as an array of IAnimal objects.
List<IAnimal> myAnimals = new List<IAnimal>();
IAnimal strayCat = new Cat("Garfield", false);
Cat myPet = new Cat("Katrina");
IAnimal myMothersDog = new Dog("Harley");
Dog myMothersOtherDog = new Dog("Cotton");
IAnimal myNeighborsDog = new SmallDog("Roxy", false);
Dog movieStarsDog = new SmallDog("Princess", true);
Dog tacoBellDog = new Chihuahua("Larry", 7);
Lion lionKing = new Lion("Simba");
myAnimals.Add(strayCat);
myAnimals.Add(myPet);
myAnimals.Add(myMothersDog);
myAnimals.Add(myMothersOtherDog);
myAnimals.Add(myNeighborsDog);
myAnimals.Add(movieStarsDog);
myAnimals.Add(tacoBellDog);
myAnimals.Add(lionKing);
string allAnimalsTalking = "";
// Create a string to return.
// Garfield says "Meow". Fido says "Woof! Woof!" etc...
for (int i = 0; i < myAnimals.Count; i++)
{
allAnimalsTalking = allAnimalsTalking + myAnimals[i].GetName() + " says \"" + myAnimals[i].Talk() + "\" ";
if (myAnimals[i] is SmallDog)
{
// Cast the IAnimal into a SmallDog object.
SmallDog yippyDog = myAnimals[i] as SmallDog;
if (yippyDog.DoesThisDogFitInAPurse())
{
allAnimalsTalking = allAnimalsTalking + " from a purse.";
}
}
}
return allAnimalsTalking;
}
}
I hope this helps some.