PHP code example of nbasnet / php-codewriter

1. Go to this page and download the library: Download nbasnet/php-codewriter 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/ */

    

nbasnet / php-codewriter example snippets


      CodeWriterSettings::create(ISyntaxGrammar::PHP, $indent = 0)

      $variable = VariableComponent::create("var")->setValue("Is Name", "string")->writeComponent();
      
OUTPUT: 
      $var = 'Is Name';

      $constant = VariableComponent::create("TEST")->setValue("VALUE 1")->makeConstant()->writeComponent();
      
OUTPUT: 
      const TEST = 'VALUE 1';
  
      $array    = ArrayComponent::create("what_is_this", TRUE)
        ->setValue([
            "string" => "is game",
            "number" => 2,
            "bool"   => FALSE,
        ])
        ->writeComponent();
        
OUTPUT:   
      $what_is_this = [
            'string' => 'is game',
            'number' => 2,
            'bool' => false,
        ];

      $function = FunctionComponent::create("myFunction")
        ->setAccessIdentifier(BaseComponent::ACCESS_PUBLIC)
        ->setParameters([ArrayComponent::create("my_array"), $variable])
        ->appendComponent($array)
        ->appendComponent($variable)
        ->writeComponent();
        
OUTPUT:
      /**
       * @param array $my_array
       * @param string $val
       */
      public function myFunction(array $my_array, $val = 'Is Name')
      {
          $what_is_this = [
              'string' => 'is game',
              'number' => 2,
              'bool' => false,
          ];
          $nischal = 'Is Name';
      }

      $class = ClassComponent::create('TestController')
        ->setExtends("Controller")
        ->appendComponent($variable)
        ->appendComponent($constant)
        ->appendBlankLine()
        ->appendComponent($array)
        ->appendBlankLine()
        ->appendComponent($function)
        ->writeComponent();
        
OUTPUT:
      /**
       * Class TestController
       */
      class TestController extends Controller
      {
           $var = 'Is Name';
           const TEST = 'VALUE 1';

           $what_is_this = [
              'string' => 'is game',
              'number' => 2,
              'bool' => false,
           ];

           /**
            * @param array $my_array
            * @param string $val
            */
           public function myFunction(array $my_array, $val = 'Is Name')
           {
               $what_is_this = [
                   'string' => 'is game',
                   'number' => 2,
                   'bool' => false,
               ];
               $nischal = 'Is Name';
           }
      }