Welcome Guest, Not a member yet? Register   Sign In
What are the differences among initialize(), constructor and early options?
#1

(This post was last modified: 01-27-2022, 04:28 AM by castle.)

Hi,
Sorry if the title is confusing, but looking at the documentation I got wondering. How are they different?
PHP Code:
class UserModel extends UserAuthModel
{
    /**
    * Called during initialization. Appends
    * our custom field to the module's model.
    */
    protected function initialize()
    {
        $this->allowedFields[] = 'middlename';
    }

PHP Code:
class UserModel extends UserAuthModel
{
    protected $allowedFields 'middlename';

PHP Code:
class UserModel extends UserAuthModel
{
    public function __construct()
    {
        $this->allowedFields[] = 'middlename';
    }

Don't they all execute the code first thing when the class is initiated?
Thanks
Reply
#2

(This post was last modified: 01-27-2022, 09:13 PM by kenjis.)

This is wrong usage. The model does not work fine. See the BaseModel::__constuct().

PHP Code:
class UserModel extends UserAuthModel
{
    public function __construct()
    {
        $this->allowedFields[] = 'middlename';
    }


The initialize() is a method called at the end of the constructor.
You can write initialization code in it.
Reply
#3

The intent of the initialize method in the Controller was solely to free up the constructor for your own usage as a developer without having to worry about the class dependencies. My advise - use class vars or the constructor and ignore the initialize method whenever possible.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB