在继承的PHP类中修改静态数组 [英] Modifying static array in inherited PHP classes

查看:120
本文介绍了在继承的PHP类中修改静态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在php中进行继承时是否有一种简单或推荐的方法来修改仅定义为静态的数组的一部分.我正在使用静态数组在对象模型中设置定义参数,以为Web服务创建XML查询数据.该特定服务对各种参数使用单一数据类型,该数据类型由代码"和与代码相关的描述"的两个值(键)组成.

I'm wondering if there is an easy or recommended way of modifying just a portion of an array defined as static when doing inheritance in php. I'm using a static array to set up definition parameters in an object model to create XML query data for a web service. This particular services uses a single datatype for various parameters that consists of two values (keys) for a 'Code' and a 'Description' relating to the code.

我的模型允许我同时指定默认"和有效"值的数组,我的代码可以在创建DOMElement时对其进行检查.当然,这种特殊用法仅由两个值组成,因此仅定义类似于CodeDescriptionType的特定类来处理这些并非难事,但是我看到为特定请求类型创建其他类似继承的类在哪里很有价值并且某些数据结构比两个键/值对更长.以下是代码的示例:

My model allows me to specify both 'default' and an array of 'valid' values that my code can check against when creating the DOMElement. Granted, this particular usage only consists of two values so it would not be incredibly difficult to just define specific classes similar to the CodeDescriptionType to handle these, but I see where it could be valuable to create other similarly inherited classes for specific request types and some of the data structures are MUCH longer than just two key/value pairs. Here's a sample of what the code looks like:

class CodeDescriptionType extends Shipping_xmlAbstract {
    public static $_elementDefs = array(
        'Code' =>
            array(
                'type' => 'string',
                'min'  => 1,
                'max'  => 1
            ),
        'Description' =>
            array(
                'type' => 'string',
                'min'  => 0,
                'max'  => 1
            )
    );
    public function __construct($name,$elem=null) {
        parent::__construct($name,null,$elem);
    }
}

我在此版本的xmlAbstract中添加了$ name,以便当多个数据元素使用相同的结构时,我只需传递该结构的键名即可基于XML创建适当的DOM元素. CodeDescriptionType.我想做的就是在'code'键下添加'default' => "value"'valid' = array(1,2,3,4)参数.如果所有其他方法都失败了,我可以向该类添加另外两个参数以传入这两个参数,但是我很想知道从父类继承时是否有一种方法可以修改静态数组的内容. (array_merge在静态上下文中不起作用)

I added $name to this version of my xmlAbstract so that when multiple data elements are using the same structure, I can just pass in the key-name for that structure to create the appropriate DOM Element in the XML based on the CodeDescriptionType. What I would like to be able to do is to add 'default' => "value" and 'valid' = array(1,2,3,4) parameters under just the 'code' key. If all else fails, I can add an additional two parameters to this class to pass in those two but I'd be curious to know if there's a way to modify the contents of a static array when inheriting from a parent class. (array_merge won't work in the static context)

推荐答案

我遇到了另一个需要为本文中提到的DOMDocument包装器创建一组新定义的需求.我仍然没有找到任何合并数组元素的内置程序,但是由于这个特定应用程序的性质,我真的不想只写标准的继承属性,因为那样会破坏围绕DOMDocument编写包装器的目的.首先. (如果我最终选择了那条路线,我将写一些东西来将PHP对象的属性转换为DOMDocument元素和属性-如果我能找到一种自动从xsd轻松构建def的方法,则可能会发生这种情况)

I ran into another need to create a new set of definitions for my DOMDocument wrapper mentioned in this post. I still haven't found any built-ins to merge array elements but due to the nature of this particular application I don't really want to just write standard inherited properties either because that would kind of defeat the purpose of writing a wrapper around DOMDocument in the first place. (If I end up going that route, I'll just write something to translate PHP object properties to DOMDocument elements and attributes - which may yet happen if I can find a way to easily build the defs from the xsd automatically)

我忘记了这个线程的存在,但是实际上,最终的结果是按照现在的答案去做,因为在使用它几年之后,我可以更好地处理PHP的作用域.我正在创建的新定义在xsd中具有许多继承(扩展)类型.因此,无论如何都需要在PHP中定义定义以帮助构建DOMDocument中的最终元素,这些元素被包装在模型中的多个级别上.

I forgot this thread existed but essentially ended up doing what the answers suggested now that I have a better handle on the scopes in PHP after working with it for a few years. The new definitions I'm creating have a number of inherited (extension) types in the xsd to start with. So defining definitions in the PHP to help build the eventual elements in the DOMDocument being wrapped at multiple levels in the models is required anyway.

在抽象的父基类(实际上是包装DOMDocument并构建各种子DOMElement碎片的肮脏工作)中,我添加了以下函数:

In the abstract parent base class (the one that actually does the dirty-work of wrapping the DOMDocument and building the various sub DOMElement pieces-parts) I added the following function:

public static function cascadeDefs($layer) {
    if(!class_exists($layer)) {
        $layerNS =  __NAMESPACE__ . '\\DataTypes\\' . $layer;
        if(class_exists($layerNS))
            $layer = $layerNS;
    }
    if(class_exists($layer))
        foreach($layer::$_elementDefs as $k => $v)
            if(!isset(static::$_elementDefs[$k]))
                static::$_elementDefs[$k] = $v;
}

我考虑过追溯以找出调用类是什么,而不是传入$ layer变量,但是发现传入类名更有用,因为它允许我也从中导入"定义PHP继承结构之外的类.

I considered having it back-trace to figure out what the calling class was rather than passing in the $layer variable, but found it more useful to pass the class name in, as it allows me to also 'import' definitions from a class outside the PHP inheritance structure.

例如,如果我有一个"customerPaymentProfileType"类型的定义,该定义与其他几个支付配置文件变体共享基本元素(customerPaymentProfileBaseType类)名称.然后是另一个具有附加参数的类型,但其他类型与customerPaymentProfileType相同.

So, for example, if I have a one definition for a 'customerPaymentProfileType' type which shares base element (customerPaymentProfileBaseType class) names with a couple of other variations of payment profiles. Then there is another type which has an additional parameter but is otherwise identical to the customerPaymentProfileType.

我用这些定义做的一件事情是分配元素名称",该元素要显示在DOMElement的最终xml输出中,该DOMElement是在基本xml的主构造函数中的最终xml的任何子分支中创建的DOMElement中显示的.抽象类. XML中的大多数基本"定义等同于PHP中的抽象类,它们不直接用于定义元素类型,而仅用作其他类型的扩展.但是,customerPaymentProfile在一个定义中直接用作XML类型. 我还有一个问题,当使用扩展(Ex)类型定义时,XML元素名称是不同的,通常我会在第一个非抽象级别传递它的名称. 因此,我可以从baseType继承(而不是从简单类型继承)(扩展主包装并包括核心元素定义),将一个唯一的扩展名添加到扩展定义中,定义[不同的]元素名称,然后导入其他参数在基本版本中.

One of the things I do with these definitions is I assign the 'element name' that is to show up in the final xml output from the DOMElement created in any sub-branch of the final xml within the main constructor of the base abstract class. Most of the 'base' definitions in the XML amount to the equivalent of an abstract class in PHP, not being used directly to define an element type but instead only serving as an extension for other types. customerPaymentProfile, however, is used directly as an XML type in one definition. I also have an issue that when the Extended (Ex) type definition is used, the XML element name is different and normally I pass the name it at the first non-abstract level. So rather than inherit from the simple type, I can inherit from baseType (which extends the main wrapper and includes the core element definitions), add the one unique to the extended definition, define the [different] element name, then import the additional parameters in the basic version.

// technically extends customerPaymentProfileType but that is not an abstract and has different naming
class customerPaymentProfileExType extends customerPaymentProfileBaseType
{
    //public static $_attrNames = array();
    public static $_elementDefs = array(
        'customerPaymentProfileId' => array(
            'type' => 'string', // numeric string
            'min' => 0
        )
    );

    /**
     * inherited child of the xmlAbstract::__construct to build a request XML data element
     * @param null|array $elem
     */
    public function __construct($elem=null) {
        parent::__construct('paymentProfile',null,$elem);
        self::cascadeDefs(get_parent_class(__CLASS__));
        // add types from customerPaymentProfileType also
        self::cascadeDefs('customerPaymentProfileType');
    }
}

在通常情况下,只需调用以下命令即可从父级导入继承的数组元素:

under normal circumstances, the inherited array elements can be imported in from the parent with just the call to:

self::cascadeDefs(get_parent_class(__CLASS__));

这篇关于在继承的PHP类中修改静态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆