Error with namespace

Asked

Viewed 93 times

-1

I have the following folder structure: Inside the Form folder there is a file called Form.php (among others). Form.php belongs to the 'Book Widgets Form' namespace. There is another folder with the name Wrapper and inside it has the Formwrapper.php file that belongs to the namespace 'Book Widgets Wrapper'. In the Formwrapper class constructor it is to receive an object from the Form class. Here comes the problem, php does not recognize the namespaces informed by the 'use' and when I run the project appears the following message:

Fatal error: Uncaught Typeerror: Argument 1 passed to Livro Widgets Wrapper Formwrapper::__Construct() must be an instance of Livro Widgets Wrapper Form, instance of Livro Widgets Form Form Given, called in C: wamp64 www projects phpoo4ed App Control Contactoform.php on line 20 and defined in C: wamp64 www projects phpoo4ed Lib Book Widgets Wrapper Formwrapper.php on line 14

Since in the Wrapper folder there is no Form file. Any solution?

use Livro\Control\ActionInterface;
use Livro\Widgets\Form\FormElementInterface;

namespace Livro\Widgets\Form;

class Form {

    protected $title;
    protected $name;
    protected $fields;
    protected $actions;

    public function __construct($name = 'my_form') {
        $this->setName($name);
    }

    public function addField($label, FormElementInterface $object, $size = '100%') {
        $object->setSize($size);
        $object->setLabel($label);
        $object->fields[$object->getName()] = $object;
    }

    public function addAction($label, ActionInterface $action) {
        $this->actions[$label] = $action;
    }

    public function setData($object) {
        foreach ($this->fields as $name => $field) {
            if ($name and isset($object->$name)) {
                $field->setValue($object->$name);
            }
        }
    }

    public function getData($class = 'stdClass') {
        $object = new $class;

        foreach ($this->fields as $key => $fieldObject) {
            $val = isset($_POST[$key]) ? $_POST[$key] : '';
            $object->$key = $val;
        }

        foreach ($$_FILES as $key => $content) {
            $object->$key = $content['tmp_name'];
        }

        return $object;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }

    public function setTitle($title) {
        $this->title = $title;
    }

    public function getTitle() {
        return $this->title;
    }

    public function getFields() {
        return $this->fields;
    }

    public function getActions() {
        return $this->actions;
    }
}


<?php
use Livro\Widgets\Base\Element;
use Livro\Widgets\Container\Panel;
use Livro\Widgets\Form\Button;
use Livro\Widgets\Form\Form;

namespace Livro\Widgets\Wrapper;

class FormWrapper {

    private $decorated;

    public function __construct(Form $form) {
        $this->decorated = $form;
    }

    public function __call($method, $parameters) {
        return call_user_func(array($this->decorated, $method), $parameters);
    }

    public function show() {
        $element = new Element('form');
        $element->class = 'form-horizontal';
        $element->enctype = 'multipart/form-data';
        $element->method = 'post';
        $element->names = $this->decorated->getName();
        $element->width = '100%';

        foreach ($this->decorated->getFields() as $field) {
            $group = new Element('div');
            $group->class = 'form-group';

            $label = new Element('label');
            $label->class = 'col-sm-2 col-form-label';
            $label->add($field->getLabel());

            $col = new Element('div');
            $col->class = 'col-sm-10';
            $col->add($field);

            $field->class = 'form-control';
            $group->add($label);
            $group->add($col);
            $element->add($group);
        }

        $group = new Element('div');
        $i = 0;
        foreach ($this->decorated->getActions() as $label => $action) {
            $name = strtolower(str_replace('', '_', $label));
            $button = new Button($name);
            $button->setFormName($this->decorated->getName());
            $button->setAction($action, $label);
            $button->class = 'btn' . (($i == 0) ? 'btn-success' : 'btn-secondary');
            $group->add($button);
            $i++;
        }

        $panel = new Panel($this->decorated->getTitle());
        $panel->addContentBody($element);
        $panel->addContentFooter($group);
        $panel->show();
    }
}

inserir a descrição da imagem aqui

1 answer

-1


The error is in the namespace declaration being after the use declarations. Just reverse it works.

namespace Livro\Widgets\Form;

use Livro\Control\ActionInterface;
use Livro\Widgets\Form\FormElementInterface;

class Form {
    ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.