Skip to content

Migration issues: v0.5.0

Nelson Martell edited this page Apr 13, 2017 · 4 revisions

From v0.4.6 (and older)

Getting/setting properties

This only applies to your custom classes extending NelsonMartell\Object class (or subclass).

Properties by default uses get|set now as getter/settter prefix (instead of get_|set_). If you had a getter method called get_Name for your Name property, will be throws an exception when you try to use it:

<?php #src/SomeClass.php

namespace MyAwesomeNamespace;

use NelsonMartell\Object;

class SomeClass extends Object
{
    public function __construct($name)
    {
        parent::__construct();
        unset($this->Name);

        $this->_name = $name;
    }

    public $Name;

    private $_name = '';

    public function get_Name()
    {
        return $this->_name;
    }
}
<?php #src/index.php

use MyAwesomeNamespace\SomeClass;

$girl = new SomeClass('Nathaly');

// This throws a BadMethodCallException:
// Unable to get the property value in "MyAwesomeNamespace\SomeClass" class.
echo $obj->Name;

Solutions

1️⃣ Rename your getter and setter methods, removing '_':

In the last SomeClass class example, you just need to rename get_Name method to getName:

<?php #src/SomeClass.php
# . . .
    public function getName()
    {
        return $this->_name;
    }
# . . . 
2️⃣ Set getterPrefix and setterPrefix properties in your constructor:

You can "Bring" back the old behavior by setting getterPrefix and setterPrefix static properties in your constructor:

<?php

namespace MyAwesomeNamespace;

use NelsonMartell\Object;
use NelsonMartell\PropertiesHandler;

class SomeClass extends Object
{
    // Why use it again? More info: http://php.net/manual/en/language.oop5.traits.php.
    use PropertiesHandler;

    public function __construct($name)
    {
        parent::__construct();
        unset($this->Name);
        static::getterPrefix = 'get_';
        // static::setterPrefix = 'set_';

        $this->_name = $name;
    }

    public $Name;

    private $_name = '';

    public function get_Name()
    {
        return $this->_name;
    }
}

NOTE: There is a bug when toString method is used on classes with 'customized' prefixes. Use solution 1️⃣ instead.