PHP code example of ralouphie / codify

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

    

ralouphie / codify example snippets




// Create a code store.
$store = new \Codify\Stores\Filesystem(
    'Some\\Name\\Space',
    'directory/to/save/code'
);

// Hook up code store to an autoloader and register it.
$autoloader = new \Codify\Autoloader($store);
$autoloader->register();

// Now you can save classes.
$class = 'Some\\Name\\Space\\Foo';
$store->save($class, 'namespace Some\\Name\\Space;
    class Foo {
        public function foo() { echo "foo"; }
    }
');

// And use the classes you save.
$instance = new $class;
$instance->foo(); // Outputs "foo".



$store = new \Codify\Stores\Filesystem(/* ... */);

// Create a compiled store that will generate code for missing classes
// under the code store's namespace.
$compiled_store = new CompiledStore($store, function ($class) {

    // Generate code.
    return $code_generated;
});

$autoloader = new \Codify\Autoloader($compiled_store);
$autoloader->register();


$missing_class = 'Some\\Name\\Space\\Bar';

// This will trigger the compiled store above.
$instance = new $missing_class;

// If the compile was successful, we can now use the object.
$instance->doSomething();

// To avoid fatal errors (compile skipped), 
// you can check if the class exists first.
if (class_exists($missing_class)) {
    // Compile was successful!
} else {
    // The compile was skipped.
}