Jump to content

Changing References to Static Classes


ThePsion5

Recommended Posts

Hi guys,I'm creating a class that I would like to be able to switch out particular implementations for by making it conform to an interface. Normally I would just do this:

class UsesIt{	function DoSomething(MyClass $Class)	{		...	}}

But the class I want to switch is a purely static class - I can't create instances of it. How do I change what static class I want something to use? Thanks in advance!

Link to comment
Share on other sites

what do you mean? an you provide the actual code so we can understand it better? By static classes you mean it doesnt change, but what class is it? PS: about your signature and the matter and anti-matter, I saw some awesome show on that once. If anti-matter even touches matter they blow up in a huge explosion :). I got to see a crater from when some anti-matter from space hit the ground, was not pretty!

Link to comment
Share on other sites

Sure, I realize now that I should have clarified.Normally, if I want to create a class that uses other classes, I would create an interface for those classes. I'm going to create a better example here:

interface Cargo{  function getDimensions($format);  function isFragile();  function getWeight($format);}class CargoCarrier{  function addCargo(Cargo $Added)  {  $Weight = $Added->getWeight('metric');  ...(do other stuff)...  }}

This way, using the (Cargo $Added) line, i'm specifying that the variable $Added must contain an object that conforms to the Cargo Interface. However, i want to use a static class the same way:

$Added->getWeight('metric');
turns into
CargoClass::getWeight('metric');
To do this, I need to change the CargoClass section of the code, but i have no idea how to do that. Does that explain my problem a bit more clearly? Thanks! :)
Link to comment
Share on other sites

All an interface does is tell what methods a class needs to implement. Your class doesn't even implement the interface, you need to specify that it does:class CargoCarrier implements Cargo{}Since you have the three methods listed in the Cargo interface, then you need to implement them all in the CargoCarrier class. The only restriction is that they need to be defined as public methods, they can't be private.http://www.php.net/manual/en/language.oop5.interfaces.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...