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

parent private property with getter #37

Open
JanGalek opened this issue Dec 3, 2018 · 4 comments
Open

parent private property with getter #37

JanGalek opened this issue Dec 3, 2018 · 4 comments
Labels
Milestone

Comments

@JanGalek
Copy link

JanGalek commented Dec 3, 2018

when I have:

<?php
declare(strict_types=1);

namespace App\Model;


abstract class AbstractModel // This need not be a abstract
{
    private $abstractPrivateProperty;

    public $abstractPublicProperty;

    protected $abstractProtectedProperty;

    public function __construct()
    {
        $this->abstractPrivateProperty = 'private';
        $this->abstractPublicProperty = 'public';
        $this->abstractProtectedProperty = 'protected';
    }

    /**
     * @return string
     */
    public function getAbstractPrivateProperty(): string
    {
        return $this->abstractPrivateProperty;
    }
}


class MainModel extends AbstractModel
{
    public $publicProperty;

    private $privateProperty;

    protected $protectedProperty;

    public function __construct()
    {
        $this->publicProperty = 'public';
        $this->privateProperty = 'private';
        $this->protectedProperty = 'protected';

        parent::__construct();
    }
}

and then I try:

<?php
require_once __DIR__ . '/vendor/autoload.php';

$model = new \App\Model\MainModel();
var_dump($model->getAbstractPrivateProperty()); // returns string(7) "private"

$serializer = new \Zumba\JsonSerializer\JsonSerializer();
$serialized = $serializer->serialize($model);
var_dump($serialized); // returns string(193) "{"@type":"App\\Model\\MainModel","publicProperty":"public","privateProperty":"private","protectedProperty":"protected","abstractPublicProperty":"public","abstractProtectedProperty":"protected"}"

/** @var \App\Model\MainModel $back */
$back = $serializer->unserialize($serialized);
var_dump($back);
/** returns
class App\Model\MainModel#11 (6) {
  public $publicProperty =>
  string(6) "public"
  private $privateProperty =>
  string(7) "private"
  protected $protectedProperty =>
  string(9) "protected"
  private $abstractPrivateProperty =>
  NULL
  public $abstractPublicProperty =>
  string(6) "public"
  protected $abstractProtectedProperty =>
  string(9) "protected"
}
*/
var_dump($back->getAbstractPrivateProperty()); // PHP Fatal error:  Uncaught TypeError: Return value of App\Model\AbstractModel::getAbstractPrivateProperty() must be of the type string, null returned

so method getAbstractPrivateProperty returns null after unserialize.

I think its, bug or not ?

@JanGalek JanGalek changed the title parent private parent private property with getter Dec 3, 2018
@jrbasso
Copy link
Member

jrbasso commented Dec 3, 2018

Thanks for reporting. It seems to be a bug. I will investigate in the next few days.

@jrbasso jrbasso added the bug label Dec 3, 2018
@JanGalek
Copy link
Author

JanGalek commented Dec 3, 2018

@jrbasso thanks.

@johnCourtier
Copy link

Reflection class will only return accessible properties by getProperties method. To get also private properties of parent, you need to getParentClass and check its properties as well (recursively).

@jrbasso
Copy link
Member

jrbasso commented Dec 3, 2018

@johnCourtier Thanks for the tip. If that's the case, it should be easy to incorporate on the lib.

@jrbasso jrbasso added this to the 3.1 milestone Jul 20, 2020
@jrbasso jrbasso modified the milestones: 3.1, 3.2 Jul 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants