English 中文(简体)
PHP - Indirect transformation of overloaded property
原标题:PHP - Indirect modification of overloaded property

我知道这个问题曾几次被问过,但是没有一个人能够真正解决工作问题。 也许可以就我的具体案件发言。

I m 建造一个地图板级,使用磁力法__get(),以装载其他物体。 它看着这样的情况:

public function __get ( $index )
{
    if ( isset ($this->vars[$index]) )
    {
        return $this->vars[$index];
    }

    // $index =  role ;
    $obj = $this->createNewObject ( $index );

    return $obj;
}

我的守则是:

$user = createObject( user );
$user->role->rolename;

迄今为止,这项工作已经展开。

但是,在试图修改名称时:

$user = createUser();
$user->role->rolename =  Test ;

之后,我有以下错误:

www.un.org/spanish/ecosoc 通知:超载财产的间接修改无效

不清楚这是否仍然是购买力平价中的一些丑恶,还是“投机行为”,但不管怎样,它都没有达到我的愿望。 这对我来说确实是一个 show。 由于我如何能改变zy装物体的特性?


EDIT:

只有当我返回一阵列时,实际问题才出现。

我举了一个例子,其中转载了问题:

http://codepad.org/T1iPZm9t”rel=“noretinger”>http://codepad.org/T1iPZm9t

你们应该真正地把这一错误看在你的购买力平价环境中。 但这里确实有意思。

我试图改变物体的财产,这给我带来了通知,可以改变超载财产。 但是,如果我在此之后重复财产,我就认为它实际上改变了价值......。 1. 真正的继承......

最佳回答

尼斯女士给我说了话。

页: 1

class Sample extends Creator {

}

$a = new Sample ();
$a->role->rolename =  test ;
echo  $a->role->rolename , PHP_EOL;
$a->role->rolename->am->love->php =  w00 ;
echo  $a->role->rolename  , PHP_EOL;
echo  $a->role->rolename->am->love->php   , PHP_EOL;

产出

test
test
w00

班次

abstract class Creator {
    public function __get($name) {
        if (! isset ( $this->{$name} )) {
            $this->{$name} = new Value ( $name, null );
        }
        return $this->{$name};
    }

    public function __set($name, $value) {
        $this->{$name} = new Value ( $name, $value );
    }



}

class Value extends Creator {
    private $name;
    private $value;
    function __construct($name, $value) {
        $this->name = $name;
        $this->value = $value;
    }

    function __toString()
    {
        return (string) $this->value ;
    }
}      

Edit : New Array Support as requested

class Sample extends Creator {

}

$a = new Sample ();
$a->role = array (
        "A",
        "B",
        "C" 
);


$a->role[0]->nice = "OK" ;

print ($a->role[0]->nice  . PHP_EOL);

$a->role[1]->nice->ok = array("foo","bar","die");

print ($a->role[1]->nice->ok[2]  . PHP_EOL);


$a->role[2]->nice->raw = new stdClass();
$a->role[2]->nice->raw->name = "baba" ;

print ($a->role[2]->nice->raw->name. PHP_EOL);

产出

 Ok die baba

Modified Class

abstract class Creator {
    public function __get($name) {
        if (! isset ( $this->{$name} )) {
            $this->{$name} = new Value ( $name, null );
        }
        return $this->{$name};
    }

    public function __set($name, $value) {
        if (is_array ( $value )) {
            array_walk ( $value, function (&$item, $key) {
                $item = new Value ( $key, $item );
            } );
        }
        $this->{$name} = $value;

    }

}

class Value {
    private $name ;
    function __construct($name, $value) {
        $this->{$name} = $value;
        $this->name = $value ;
    }

    public function __get($name) {
        if (! isset ( $this->{$name} )) {
            $this->{$name} = new Value ( $name, null );
        }

        if ($name == $this->name) {
            return $this->value;
        }

        return $this->{$name};
    }

    public function __set($name, $value) {
        if (is_array ( $value )) {
            array_walk ( $value, function (&$item, $key) {
                $item = new Value ( $key, $item );
            } );
        }
        $this->{$name} = $value;
    }

    public function __toString() {
        return (string) $this->name ;
    }   
}
问题回答

你们都需要做的是,在您的“植被功能”之前添加“和;”作为参考。

public function &__get ( $index )

Struggled with this one for a while.

我怀有同样的错误,没有你的整个法典,很难确切地确定如何加以确定,而是由没有固定功能造成的。

The way that I have gotten around it in the past is I have done things like this:

$user = createUser();
$role = $user->role;
$role->rolename =  Test ;

现在请你这样做:

echo $user->role->rolename;

页: 1 试验

虽然我很晚才参加这次讨论,但我认为这或许对今后某个人有用。

我面临类似的情况。 对那些想不确定和重新确定变量的人来说,最容易的工作就是这样做。 我确信,从其他答复和《综合手册》中可以清楚地看出为什么不这样做。 为我工作的最简单工作是:

假设:

  1. $object is the object with overloaded __get and __set from the base class, which I am not in the freedom to modify.
  2. shippingData is the array I want to modify a field of for e.g. :- phone_number

 

// First store the array in a local variable.
$tempShippingData = $object->shippingData;

unset($object->shippingData);

$tempShippingData[ phone_number ] =  888-666-0000  // what ever the value you want to set

$object->shippingData = $tempShippingData; // this will again call the __set and set the array variable

unset($tempShippingData);

说明:这一解决办法是迅速解决该问题并获得可变复制的解决方案之一。 如果阵列过于hum,则最好强制改写__get/code>方法,以退回一个比较昂贵的大型阵列。

我收到这一通知是为了这样做:

$var = reset($myClass->my_magic_property);

其中规定:

$tmp = $myClass->my_magic_property;
$var = reset($tmp);

之所以出现这种情况,是因为PHP如何处理超负荷的房产,因为这些财产不易腐化或无法参照。

manual,以获得更多有关超负荷的信息。

为解决这一问题,您可使用<代码>__set功能,或创建createObject方法。

下面是<条码>__get和__set,为与您类似的情况提供一个工作环境,你可以简单修改<条码>__>><>>>>>>,以适应您的需要。

__get 实际上,从来不复一变量。 而一旦你在你的物体上设定了变数,就不再超负荷。

/**
 * Get a variable in the event.
 *
 * @param  mixed  $key  Variable name.
 *
 * @return  mixed|null
 */
public function __get($key)
{
    throw new LogicException(sprintf(
        "Call to undefined event property %s",
        $key
    ));
}

/**
 * Set a variable in the event.
 *
 * @param  string  $key  Name of variable
 *
 * @param  mixed  $value  Value to variable
 *
 * @return  boolean  True
 */
public function __set($key, $value)
{
    if (stripos($key,  _ ) === 0 && isset($this->$key)) {
        throw new LogicException(sprintf(
            "%s is a read-only event property", 
            $key
        ));
    }
    $this->$key = $value;
    return true;
}

Which will allow for:

$object = new obj();
$object->a = array();
$object->a[] = "b";
$object->v = new obj();
$object->v->a = "b";

我已经陷入了与W00同样的问题,但我没有机会重塑这一问题的构成部分的基本功能。 I ve得以用ArrayObject代替基本类型array(。 这将退回一个标的,而该标的将以提及方式归还。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签