PHP code example of kanel / phpclasseditor

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

    

kanel / phpclasseditor example snippets


$classEditor = new ClassEditor('path_to_php_class_file');
$classEditor->useIndentation(Indentation::TABS, 1);

$classEditor = new ClassEditor('path_to_php_class_file');
$classEditor->addProperty(new Property('property1', Visibility::PROTECTED));
$classEditor->addProperty(new Property('property2', Visibility::PUBLIC));
$classEditor->addProperty(new Property('property3', Visibility::PRIVATE));

$classEditor->save();

class MyClass {
    
    protected $property1;
    public $property2;
    private $property3;
    ...

}

$property = new Property('myProperty');

class MyClass {
    
    $myProperty;
    ...

}

$property = new Property('myProperty', Visibility::PROTECTED);

$property = new Property('myProperty', Visibility::PROTECTED, Value::NULL);

class MyClass {
    
    protected $myProperty = null;
    ...

}

$property = new Property('myProperty', Visibility::PROTECTED, Value::NULL, true);

class MyClass {
    
    protected static $myProperty = null;
    ...

}

$property = new Property('myProperty');
$property->setVisibility(Visibility::PUBLIC);
$property->setDefaultValue('1');
$property->setIsStatic(true);

class MyClass {
    
    public static $myProperty = 1;
    ...

}

$class = new ClassEditor($this->phpFileName);
$class->addMethod(new Method('bar', Visibility::PROTECTED));
$class->addMethod(new Method('foo', Visibility::PROTECTED));
$classEditor->save();

class MyClass {
    
    ....
    
    protected function bar()
    {
    
    }
    
    protected function foo()
    {
    
    }

}

$method = new Method('bar', Visibility::PUBLIC);
$method->setIsStatic(true);
$method->setIsFinal(true);
$method->setIsAbstract(true);
$method->setReturnType(Type::INT);
$method->setDocComment('This is my bar function');

class MyClass {
    
    ....
    
    /**
     * This is my bar function
     * @return int
     */
    abstract public static function bar(): int
    {
    
    }

}

$method = new Method('bar', Visibility::PUBLIC);
$method->setDocComment('This is my bar function');
$method->setReturnType(Type::ARRAY);
$method->addParameter(new MixedParameter('param1'));
$method->addParameter(new IntParameter('param2', 1));
$method->addParameter(new ClassParameter('param3', MyClass::class, Value::NULL));

class MyClass {
    
    ....
    
    /**
     * This is my bar function
     * @param mixed $param1
     * @param int $param2
     * @param My\NAMESPACE\MyClass $param3
     * @return array
     */
    public function bar($param1, int $param2 = 1, My\NAMESPACE\MyClass $param3 = null): array
    {
    
    }

}