PHP code example of mjamilasfihani / conquer-container

1. Go to this page and download the library: Download mjamilasfihani/conquer-container 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/ */

    

mjamilasfihani / conquer-container example snippets


namespace App\Controllers;

use App\Controllers\BaseController;
use App\Libraries\ExampleLibrary;

class Home extends BaseController
{
    /**
     * @var \App\Libraries\ExampleLibrary
     */
    protected ExampleLibrary $exampleLibrary;

    // This is your old constructor isn't?
    //
    // /**
    //  * Constructor
    //  */
    // public function __construct()
    // {
    //     $this->exampleLibrary = new ExampleLibrary();
    // }

    /**
     * This will be your new Constructor
     *
     * @param \App\Libraries\ExampleLibrary $exampleLibrary
     */
    public function __construct(ExampleLibrary $exampleLibrary)
    {
        $this->exampleLibrary = $exampleLibrary;
    };
    
    /**
     * Display Homepage
     *
     * @return string
     */
    public function index(): string
    {
        // even it has equal result, depend how like you call your library :)
        $this->exampleLibrary;

        return view('welcome_message');
    }
}

namespace App\Libraries;

use App\Libraries\ExampleLibrary;

class AnotherExampleLibrary
{
    protected ExampleLibrary $exampleLibrary;

    /**
     * Constructor
     *
     * @param \App\Libraries\ExampleLibrary $exampleLibrary
     */
    public function __construct(ExampleLibrary $exampleLibrary)
    {
        $this->exampleLibrary = $exampleLibrary;
    }

    public function anotherExampleMethod()
    {
        // you have a power from your parent class
        $exampleLibrary = $this->exampleLibrary;

        ...
    }
}

public function __construct(AnotherExampleLibrary $anotherExampleLibrary)
{
    // this use case is very help full for implement the repository pattern
    $this->anotherExampleLibrary = $anotherExampleLibrary;
}