PHP code example of melchiorkokernoot / laravel-autowire-config

1. Go to this page and download the library: Download melchiorkokernoot/laravel-autowire-config 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/ */

    

melchiorkokernoot / laravel-autowire-config example snippets


class Foo {
    public function __construct(
        #[Config('app.name')]
        public string $myConfiguredAppName,
    ){}
}

return [
    //Either AttributeStrategy::class  or AutowiredPropNameStrategy::class
    'strategy' => PropNameStrategy::class,
];

$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__),
);

use MelchiorKokernoot\LaravelAutowireConfig\Application;

$app = new Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__),
);

class Foo {
    public function __construct(
        #[Config('app.name','default value')]
        public string $myConfiguredAppName,
    ){}
}

class Foo implements AutowiresConfigs{
    public function __construct(
        #[StringConfig('app.name')]
        public StringConfig $appName,
    ){}
}

$foo = new Foo(config('app.name'));

class Foo implements AutowiresConfigs{
    public function __construct(
        public StringConfig $appName,
    ){}
}

$foo = new Foo(config('app.name'));

class Foo implements AutowiresConfigs{
    public function __construct(
        public StringConfig $appName,
    ){}

    public function bar(){
        //Casting to string
        (string) $this->appName
        
        //Shorthand method call
        $this->appName->value->v();
        
        //Shorthand property access
        $this->appName->value->v;
        
        //Ordinary method call
        return $this->appName->value();
    }
}
bash
php artisan vendor:publish --provider="MelchiorKokernoot\LaravelAutowireConfig\LaravelAutowireConfigServiceProvider"