PHP code example of offworks / classgen

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

    

offworks / classgen example snippets


echo $class;

// sample
$method = $class->addMethod('setAtPublished');

$method->comment('Publish the article')
    ->returnAs('self')
    ->initialize(function($published = true)
    {
        // mark this article as published
        $this->setPublished($published ? 1 : 0);
        
        return $this;
    });

/**
 * Publish the article
 *
 * @param $published
 * @return self
 */
public function setAsPublished($published = true)
{
    // mark this article as published
    $this->setPublished($published ? 1 : 0);
    
    return $this;
}

/**
 * Check whether article is published
 *
 * @return bool
public function isPublished()
{
    return $this->isPublished == true;
}

$method = $class->addMethod('isPopular')->returnAs('bool');

// code() method let you code within a safe handler. (enough with pollution)
$method->code(function($code)
{
    $code->addBlock('if($this->likes > 1000)', function($code)
    {
         $code->addBlock('if($this->comments->count > 100)', function($code)
         {
             $code->write('return true;');
         });
    });
    
    // continued block will skip the line break
    $code->addContinuedBlock('else if($this->isDeleted())', function($code)
    {
        $code->write(function()
        {
            throw new \Exception('The article has been deleted. Throw! throw!');
        });
    });
    
    $code->write('return false;');
});

file_put_contents(__DIR_.'/app/MyClass.php', $class->toString());

$generator = new \Classgen\Generator\Psr4('Acme\\', __DIR__.'/src');

/src
  /Model
    Article.php
  /Routes
    AdminRoute.php