PHP code example of matthew-p / yii2-services

1. Go to this page and download the library: Download matthew-p/yii2-services library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

matthew-p / yii2-services example snippets


use MP\Services\BaseModelService;

/**
 * ...
 *
 * @property SampleModel $model
 */
class MyCustomService extends BaseModelService
{
    /**
     * My simple method
     *
     * @return array
     */
    public function getSampleMethod(): array
    {
        return [];
    }
}

...

use MP\Services\ImplementServices;

/**
 * Use services in model
 * ...
 *
 * Services
 * @property MyCustomService $customService
 */
class SampleModel extends ActiveRecord
{
    use ImplementServices;
    
    /**
     * @inheritdoc
     */
    public static function services(): array
    {
        return [
            'customService' => MyCustomService::class,
        ];
    }
    
    ...
}

$model = new SampleModel();
$model->customService->getSampleMethod();