Considering this basic code snippet:
<?
class mcClassington
{
var $mcProperty='default';
function mcDoSomething()
{
if($this->mcProperty == (/* new type of same class */)->mcProperty)
{
sameBusiness();
}
else
{
sortIt($this->mcProprety);
}
}
}
Not sure how I can ask for a new object of whichever type the $this instance is.
I’m trying to write this function for a base class but let classes which extend from it check against their own default values. All I could think of was super-ghetto using switch(get_class($this)) but that would completely destroy the dynamic goal of the code I’m working on.
EDIT: So,.. I can’t actually post the complete real code. The best I can provide is a static example of usage posted below.
class displayObject //implements attachable
{
public $layer = null;
public $xPos = Array(
'left'=>null,
'width'=>null,
'margin-left'=>null,
'margin-right'=>null,
'padding-left'=>null,
'padding-right'=>null
);
public $yPos = Array(
'top'=>null,
'height'=>null,
'margin-top'=>null,
'margin-bottom'=>null,
'padding-top'=>null,
'padding-bottom'=>null
);
}
class Button extends displayObject
{
public $link, $image, $backing;
public $xPos = Array(
'left'=>null,
'width'=>'120px',
'margin-left'=>'8px',
'margin-right'=>'8px',
'padding-left'=>null,
'padding-right'=>null
);
public $yPos = Array(
'top'=>null,
'height'=>'108px',
'margin-top'=>'4px',
'margin-bottom'=>'4px',
'padding-top'=>null,
'padding-bottom'=>null
);
}
And then, in other files people might use the class like so:
$navBar = Array(); // Will hold basic Button Objects
$specialtyButton = new Button();
$specialtyButton->$xPos['width'] = '220px';
$specialtyButton->$yPos['height'] = '220px';
/* and so on... */
The function I’m writing must be called from within the base class displayObject in the format myFunction($this); What I’m trying to solve is sorting the pseudo-styles into different places based on whether or not they are the same as their default values.
For instance, $specialtyButton would be sorted specially because it’s width and height differed from Button’s default values but the Buttons inside $navBar would be overlooked because nothing was set explicitly.
You can simply:
…but I wouldn’t. I would say it would be more sensible to hold the default values in a
privateproperty, so no-one can modify them, and compare against them instead. Creating new instances just to check if values are default seems like a horrible thing to do to me…