如何将子类与 PHP 5.2 中基类内的调用的默认值进行比较?
考虑这个基本代码片段:
<?
class mcClassington
{
var $mcProperty='default';
function mcDoSomething()
{
if($this->mcProperty == (/* new type of same class */)->mcProperty)
{
sameBusiness();
}
else
{
sortIt($this->mcProprety);
}
}
}
不确定如何请求 $this
实例的类型的新对象。
我试图为基类编写这个函数,但让从它扩展的类检查它们自己的默认值。我能想到的就是使用 switch(get_class($this)) 进行超级贫民区,但这会完全破坏我正在编写的代码的动态目标。
编辑:所以,..我实际上无法发布完整的真实代码。我能提供的最好的就是下面发布的静态用法示例。
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
);
}
然后,在其他文件中,人们可能会像这样使用该类:
$navBar = Array(); // Will hold basic Button Objects
$specialtyButton = new Button();
$specialtyButton->$xPos['width'] = '220px';
$specialtyButton->$yPos['height'] = '220px';
/* and so on... */
我正在编写的函数必须以 myFunction($this); 的格式从基类 displayObject 中调用。 code> 我想要解决的问题是根据伪样式是否与其默认值相同将它们排序到不同的位置。
例如, $specialtyButton 将被特殊排序,因为它的宽度和高度与 Button 的默认值不同,但 $navBar 内的按钮将被忽略,因为没有明确设置任何内容。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以简单地:
……但我不会。我想说,将默认值保存在
private
属性中会更明智,这样就没有人可以修改它们并与它们进行比较。创建新实例只是为了检查值是否是默认值对我来说似乎是一件可怕的事情......You can simply:
...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...我会将您的格式更改为如下所示:
基本上,要么将
default
值定义为同一类成员内的数组键,要么为默认值创建另一个类成员,例如private $ mcPropertyDefault;
。I would change your format to something like this:
Basically, either define the
default
value as an array key inside the same class member, or create another class member for the default value, likeprivate $mcPropertyDefault;
.尝试在继承对象的子类中使用关键字parent 示例:
父类将指向父类,但不确定它是否可以像使用方法一样使用属性,因为我只能使用它当我试图覆盖它的一些方法并且我想向它添加一个新进程时,用于从父类访问原始方法的关键字,但如果这不起作用,我认为另一个最好的选择是使用以下概念封装创建设置和获取属性值的方法并使该属性的名称与子类的名称不同,以保留父类的默认值,然后在设置器上您现在可以检查要设置的值是否是默认值。
try to use the keyword parent in your child class that is inheriting the object example:
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.