PHP code example of samejack / point-core

1. Go to this page and download the library: Download samejack/point-core 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/ */

    

samejack / point-core example snippets




oint\core\Context;
use point\core\Bean;

class Bar
{
  public function __toString()
  {
    return 'Bar!';
  }
}

$context = new Context();

$context->addConfiguration(array(
  array(
    Bean::CLASS_NAME => 'Bar'
  )
));

$bar = $context->getBeanByClassName('Bar');
echo $bar;  // print Bar!



use point\core\Context;
use point\core\Bean;

class Foo
{
  /**
   * @Autowired
   * @var Bar
   */
  private $_bar;

  public function getBar()
  {
      return $this->_bar;
  }
}

class Bar
{
}

$context = new Context();

$context->addConfiguration(array(
  array(
    Bean::CLASS_NAME => 'Foo'
  ),
  array(
    Bean::CLASS_NAME => 'Bar'
  )
));

$foo = $context->getBeanByClassName('Foo');
var_dump($foo->getBar());  // print Class Bar



use point\core\Context;
use point\core\Bean;

class Foo
{
  /**
   * @Autowired
   * @var Bar
   */
  private $_bar;

  public function getBar()
  {
      return $this->_bar;
  }
}

$context = new Context();

$context->addConfiguration(array(
  array(
    Bean::CLASS_NAME => 'Foo'
  )
));

$foo = $context->getBeanByClassName('Foo');

var_dump($foo->getBar());  // print NULL on unload Bar Class

// load Bar class
class Bar
{
}

// set configuration
$context->addConfiguration(array(
    array(
        Bean::CLASS_NAME => 'Bar'
    )
));

var_dump($foo->getBar());  // print Class Bar


use point\core\Context;
use point\core\Bean;

class Foo
{
    /**
     * @Qualifier("bar.2")
     * @var Bar
     */
    private $_bar;
    public function getBar()
    {
        return $this->_bar;
    }
}
class Bar
{
    private $_name;
    public function __construct($name)
    {
        $this->_name = $name;
    }
    public function toString()
    {
        return $this->_name;
    }
}
$context = new Context();
$context->addConfiguration(array(
    array(
        Bean::CLASS_NAME => 'Foo'
    ),
    array(
        Bean::CLASS_NAME => 'Bar',
        Bean::ID => 'bar.1',
        Bean::CONSTRUCTOR_ARG => ['i am first.']
    ),
    array(
        Bean::CLASS_NAME => 'Bar',
        Bean::ID => 'bar.2',
        Bean::CONSTRUCTOR_ARG => ['i am second.']
    )
));
$foo = $context->getBeanByClassName('Foo');
var_dump($foo->getBar());  // print Class Bar

class MyControllerA
{
    /**
    * @Autowired
    * @var PDO
    */
    private $_pdo;
    public function getPdo()
    {
        return $this->_pdo;
    }
}

class MyControllerB
{
    /**
    * @Autowired
    * @var PDO
    */
    private $_pdo;
    public function getPdo()
    {
        return $this->_pdo;
    }
}

$context = new Context();
$context->addConfiguration(array(
    array(
        Bean::CLASS_NAME => 'MyControllerA'
    ),
    array(
        Bean::CLASS_NAME => 'MyControllerB'
    ),
    array(
        Bean::CLASS_NAME => 'PDO',
        Bean::CONSTRUCTOR_ARG => ['mysql:host=localhost;dbname=mysql', 'root', 'password!']
    )
));

$ctrlA = $context->getBeanByClassName('MyControllerA');
var_dump($ctrlA->getPdo());  // print: class PDO#11 (0)...
$ctrlB = $context->getBeanByClassName('MyControllerB');
var_dump($ctrlB->getPdo());  // print: class PDO#11 (0)...