Skip to content

Conversation

@ghostwriter
Copy link

Description

Add PhpNamespace classes to a PhpFile class.

  • bug fix? no
  • new feature? yes
  • BC break? no

Example

<?php declare(strict_types=1); 

use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpFile;
use Nette\PhpGenerator\PhpNamespace;

$phpFile= new PhpFile();
$namespace = new PhpNamespace('FooBar');
$class = new ClassType('Foo');

$method = $class->addMethod('baz');
$method->setReturnType('FooBar\\Bar');
$method->setBody('return new Bar();');

$namespace->add($class);
$namespace->add(new ClassType('Bar'));

$phpFile->add($namespace);

echo $phpFile;

Result:

<?php

namespace FooBar;

class Foo
{
    public function baz(): Bar
    {
        return new Bar();
    }
}

class Bar
{
}

closes #67

Nathan E added 3 commits October 11, 2020 18:31
@dg
Copy link
Member

dg commented Oct 13, 2020

Thanks

@dg dg merged commit c9480f7 into nette:master Oct 13, 2020
@ghostwriter ghostwriter deleted the feature-create-phpfile-from-phpnamespace branch October 13, 2020 17:48
dg pushed a commit that referenced this pull request Oct 21, 2020
dg pushed a commit that referenced this pull request Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: add PhpNamespace to PhpFile

2 participants