D
D
dev4002016-09-09 15:49:08
PHP
dev400, 2016-09-09 15:49:08

Is it forgivable here a little bit of static?

It's about the $meta property

<?php
namespace app\Common\View;

class BaseView implements ViewInterface
{
  use ViewTrait;

  public $data = [];
  public $layout = '/layouts/blog';
    public $layoutPath;


  private static $meta = [];


    public static function setMeta($key, $value) {

        self::$meta[$key] = $value;

    }

    public static function getMeta($key) {

        return self::$meta[$key];

    }


    public function setLayout( $layout = null) {

        if ( !is_null($layout) ) {
            $this->layout = $layout;
        }

    }


    public function set($property, $value) {

        $this->{$property} = $value;

    }

    public function get($property) {

        return $this->{$property};

    }


    public function layoutPath($path) {

        return __DIR__ . '/app/web' . $path . ".php";

    }

    
    public function render($template, $data = []) {

        ( is_null($data) ) ?: extract($data);

    ob_start();

    if ( file_exists( $this->layoutPath($template) ) ) {

      require $this->layoutPath($template);

    } else {
      
      echo 'Template not found!';

    }

    return ob_get_clean();
  }

}

In the view I set it
//
use app\Common\View\BaseView as View;
//
View::setMeta('title', 'Страница');

And in the layout I display it with a getter
//
use app\Common\View\BaseView as View;
//
<title><?= View::getMeta('title') ?></title>

That is, BaseView::$meta works outside of the object. How bad is that, do you think?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antiless, 2016-09-09
@dev400

I think it's bad. One page - one object incl. with one meta. And it is not clear why exactly the statics are here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question