سؤال

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:

$myClassName = get_class($this);
$newObj = new $myClassName;
if ($this->mcProperty == $newObj->mcProperty) {
  // ...

...but I wouldn't. I would say it would be more sensible to hold the default values in a private property, 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...

نصائح أخرى

I would change your format to something like this:

class mcClassington
{
    private $mcProperty = array( 'value' => '', 'default' => 0);

    function mcDoSomething()
    {
        if($this->mcProperty['value'] == $this->mcProperty['default'])
        {
        }
    }
}

Basically, either define the default value as an array key inside the same class member, or create another class member for the default value, like private $mcPropertyDefault;.

try to use the keyword parent in your child class that is inheriting the object example:

parent::xPos;

the parent will point to the parent class but not sure if it would work with properties the way it would work with methods though cause I was only able to use this keyword for accessing the original method from the parent class when I'm trying to override some of it's methods and I want to add a new process to it, but if that won't work I think another best option is to use the concept of encapsulation creating a method for setting and getting the property value and making the name of that property different of that from your child class to preserve the default value from the parent class, then on the setter you could now check whether the values to be set are default or not.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top