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

unset object property makes it private #15276

Closed
konsultaner opened this issue Feb 4, 2021 · 9 comments · Fixed by #15719
Closed

unset object property makes it private #15276

konsultaner opened this issue Feb 4, 2021 · 9 comments · Fixed by #15719
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report documentation Documentation required good first issue A good fit for someone who is new to the project and looking to contribute for the first time status: medium Medium

Comments

@konsultaner
Copy link

I'm using PHP 7.4 with phalcon 4.1.0. The following code raises an Exception:

$phalconModel->property = "foo";
unset($phalconModel->property); 
$phalconModel->property = "bar"; //<- This throws
Fatal error: Uncaught Phalcon\Mvc\Model\Exception: Cannot access property 'XXX' (not public)

With phalcon 4.0.5 on PHP 7.3 it worked as expected. I'm not sure if this is a PHP issue or Phalcon. You may aswell close it if this is not Phalcon related.

@Jeckerson
Copy link
Member

Jeckerson commented Feb 4, 2021

Hello @konsultaner

I can't reproduce your error

$user = User::findFirst(1);
var_dump($user->id); // int(1)
unset($user->id);
var_dump($user->id); // Notice: Access to undefined property User::id

$user->custom = 'value';
var_dump($user->custom); // string(5) "value"
unset($user->custom);
var_dump($user->custom); // Notice: Access to undefined property User::custom

PHP version:
php -v

PHP 7.4.12 (cli) (built: Oct 31 2020 17:04:09) ( NTS )

Phalcon Version:
php -r 'echo Phalcon\Version::get();'

4.0.6

@konsultaner
Copy link
Author

ok, that is strange, but this is what happens on my machine. I'll try more investigations on this.

@Jeckerson
Copy link
Member

Didn't notice that you are using version 4.1.0. It seems due Zephir version

https://github.com/phalcon/zephir/blob/master/CHANGELOG.md#01218---2020-04-25

Zephir ignored property visibility and has not thrown error when setting private/protected properties in scope that shouldn't
intend for it #2078, #14810, #14766

Try to make properties public, ou use getters

@Jeckerson
Copy link
Member

And this is correct PHP behavior, which was fixed after 4.1.0:

<?php

class Test
{
    protected $protected = 1;
    public $public = 2;
    private $private = 3;
}

$class = new Test();

var_dump($class->public);
unset($class->public);
var_dump($class->public); // Notice: Undefined property: Test::$public

unset($class->protected); // Fatal error: Uncaught Error: Cannot access protected property Test::$protected

Sandbox - http://sandbox.onlinephpfunctions.com/code/efae33dd9145a4420dc82e676716d61517d36b2c

@Jeckerson Jeckerson added the not a bug Reported issue is not a bug label Feb 4, 2021
@konsultaner
Copy link
Author

konsultaner commented Feb 5, 2021

@Jeckerson I don't know what went wrong, but my field was public, but what I did was:

<?php

class Test extends Model // Its a phalcon model
{
    protected $protected = 1;
    public $public = 2;
    private $private = 3;
}

$class = new Test();

var_dump($class->public);
unset($class->public);
var_dump($class->public); // Notice: Undefined property: Test::$public

$class->public = 3; // Fatal error: Uncaught Phalcon\Mvc\Model\Exception: Cannot access property 'public' (not public)

@Jeckerson Jeckerson added documentation Documentation required status: unverified Unverified 5.0 The issues we want to solve in the 5.0 release and removed not a bug Reported issue is not a bug labels Mar 26, 2021
@stale stale bot added the stale Stale issue - automatically closed label Aug 11, 2021
@Jeckerson Jeckerson removed the stale Stale issue - automatically closed label Aug 11, 2021
@phalcon phalcon deleted a comment from stale bot Aug 12, 2021
@niden niden added the bug A bug report label Aug 12, 2021
@Jeckerson
Copy link
Member

Jeckerson commented Sep 17, 2021

To fix this, need to review this x2 blocks of code:

        if property_exists(this, property) {
            let manager = this->getModelsManager();

            if unlikely !manager->isVisibleModelProperty(this, property) {
                throw new Exception(
                    "Cannot access property '" . property . "' (not public)."
                );
            }
        }
    final public function isVisibleModelProperty(<ModelInterface> model, string property) -> bool
    {
        var properties, className;

        let className = get_class(model);

        if !isset this->modelVisibility[className] {
            let this->modelVisibility[className] = get_object_vars(model);
        }

        let properties = this->modelVisibility[className];

        return array_key_exists(property, properties);
    }

Unset property will exist even after

<?php

class Test
{
    public $public;
    protected $protected;
    private $private;
}

$test = new Test();

var_dump(property_exists($test, 'public')); // true
unset($test->public);
var_dump(property_exists($test, 'public')); // true

@Jeckerson Jeckerson added good first issue A good fit for someone who is new to the project and looking to contribute for the first time and removed status: unverified Unverified labels Sep 17, 2021
@Jeckerson Jeckerson added the hacktoberfest See https://hacktoberfest.digitalocean.com/ for participants details label Oct 3, 2021
@OliverMensahDev
Copy link

@Jeckerson, is someone working on this issue?

@BeMySlaveDarlin
Copy link
Contributor

@Jeckerson, is someone working on this issue?

Im on it, will be fixed asap

BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
BeMySlaveDarlin pushed a commit to BeMySlaveDarlin/cphalcon that referenced this issue Oct 21, 2021
@niden niden linked a pull request Oct 21, 2021 that will close this issue
5 tasks
@niden niden added status: medium Medium and removed hacktoberfest See https://hacktoberfest.digitalocean.com/ for participants details labels Oct 21, 2021
@niden
Copy link
Member

niden commented Oct 21, 2021

Resolved in #15719

@niden niden closed this as completed Oct 21, 2021
@niden niden moved this to Released in Phalcon v5 Aug 25, 2022
@niden niden added this to Phalcon v5 Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release bug A bug report documentation Documentation required good first issue A good fit for someone who is new to the project and looking to contribute for the first time status: medium Medium
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants