Get var inside behavior

How can I set var $types inside static method ‘create’ so that my attached behavior can use its value later on saving model Photo?


class Photo extends ActiveRecord

{

    private static $types;


    public static function create($file, $title, $alt, $type): self

    {

        $photo = new static();

        $photo->file = $file;

        $photo->title = $title;

        $photo->alt = $alt;

        self::$types = $type;

        return $photo;

    }


    public function behaviors()

    {

        print_r(self::$types);


        return [ 'some behavior attributes' ];

    }

}

here you go




<?php

class Photo extends ActiveRecord

{

    public $types;


    public static function create($file, $title, $alt, $type): self

    {

        $photo = new static();

        $photo->file = $file;

        $photo->title = $title;

        $photo->alt = $alt;

        // set types property

        $photo->types = $type;

        return $photo;

    }


    public function behaviors()

    {

        print_r($this->types);

        return [ 'some behavior attributes' ];

    }

}

note: your behaviors might be attached before you call create so be careful yii provides access to the context object through $this->owner property inside the behavior allows you to access the owner object.

Thank you, I will note ‘$this->owner property’ tip.