Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot write property #28

Closed
RV7PR opened this issue Feb 22, 2019 · 9 comments
Closed

Error: Cannot write property #28

RV7PR opened this issue Feb 22, 2019 · 9 comments

Comments

@RV7PR
Copy link

RV7PR commented Feb 22, 2019

I have a class that extends mysqli class and those properties are defined as public but are actually read only properties so I have to put $property->setValue() and $property->getValue() inside a try and catch block for it to work.

Example:

do{
    foreach ($reflection->getProperties() as $property){
        if($property->isStatic()){
            continue;
        }
        $property->setAccessible(true);
        $value = $property->getValue($instance);
        if(is_array($value) || is_object($value)){
            $this->mapByReference($value);
        }

        try {
            $property->setValue($data, $value);
        }
        catch(\Exception $e) {}
        catch(\Throwable $e) {}
    }
} while($reflection = $reflection->getParentClass());

and

do{
    if(!$reflection->isUserDefined()){
        break;
    }
    foreach ($reflection->getProperties() as $property){
        if($property->isStatic()){
            continue;
        }
        $property->setAccessible(true);

        try {
            $item = $property->getValue($data);
        } catch(\Exception $e) {
            continue;
        } catch(\Throwable $e) {
            continue;
        }

        if ($item instanceof SerializableClosure || ($item instanceof SelfReference && $item->hash === $this->code['self'])) {
            $this->code['objects'][] = array(
                'instance' => $data,
                'property' => $property,
                'object' => $item instanceof SelfReference ? $this : $item,
            );
        } elseif (is_array($item) || is_object($item)) {
            $this->mapPointers($item);
            $property->setValue($data, $item);
        }
    }
} while($reflection = $reflection->getParentClass());
@sorinsarca
Copy link
Member

@rowhel95 please provide a code snippet so we can reproduce your problem

@RV7PR
Copy link
Author

RV7PR commented Feb 22, 2019

@sorinsarca
Copy link
Member

That is not related to opis/closure, please provide a php code that reproduces your problem when you try to serialize/unserialize something using opis/closure.

@msarca
Copy link
Member

msarca commented Feb 22, 2019

You cannot serialize database connections in PHP. The try-catch thing will not fix this, it will only hide it. I'm closing this issue because it has nothing to do with opis/closure.

@msarca msarca closed this as completed Feb 22, 2019
@RV7PR
Copy link
Author

RV7PR commented Feb 22, 2019

Error: Cannot write property in /vendor/opis/closure/src/SerializableClosure.php:625
Stack trace:
#0 /vendor/opis/closure/src/SerializableClosure.php(625): ReflectionProperty->setValue(Object(database), 1)
#1 /vendor/opis/closure/src/SerializableClosure.php(581): Opis\Closure\SerializableClosure->mapByReference(Object(database))
#2 /vendor/opis/closure/src/SerializableClosure.php(148): Opis\Closure\SerializableClosure->mapByReference(Array)
#3 [internal function]: Opis\Closure\SerializableClosure->serialize()
#4 /class.base_table.php(2650): serialize(Object(Table\Sql))
#5 /class.base_table.php(493): framework\base_table->save()

@sorinsarca
Copy link
Member

@rowhel95 that is the stack trace only, I want to generate that trace on my computer (in other words I want to reproduce the problem), so I needed an example of PHP code.

Anyway, as @msarca said, what are you trying to do is not possible in PHP.

@RV7PR
Copy link
Author

RV7PR commented Feb 22, 2019

@msarca @sorinsarca

create a class named for example database that extends mysqli

class database extends mysqli

now run this code

$oDatabase = new database();

serialize($oDatabase);

$test = function() use ($oDatabase) {

};

$test = new \Opis\Closure\SerializableClosure($test);

serialize($test);

you will see that serializing with php works just fine but using it for closure gives exception

@msarca
Copy link
Member

msarca commented Feb 22, 2019

I was able to reproduce this and I will provide a fix.

@msarca msarca reopened this Feb 22, 2019
@msarca
Copy link
Member

msarca commented Feb 22, 2019

This is fixed in v3.1.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants