【问题标题】:PHP OOP pass variables to sub classesPHP OOP 将变量传递给子类
【发布时间】:2019-07-25 02:20:32
【问题描述】:

我在构造函数中设置变量值。
为了保持一切井井有条,我创建了其他类,它们只会在“应用程序”类中实例化一次。
我想将受保护的变量值传递给其他类(前端,后端......)。我知道我们可以在这些类中创建相同的变量并将变量作为参数传递。这会导致大量的代码重复。
有没有更好的办法?
谢谢

class application{

    protected $name;
    protected $version;
    protected $slug;

    public function __construct(){

        $this->name = $name;
        $this->version = $version;
        $this->slug = $slug;

        $this->includes();
    }

    public function create_settings(){
        //Only one instantiation
        $frontend = new Frontend_Settings();
        $backend = new Backend_Settings;
        //.. more like these
    }

}

class Frontend_Settings{
    public function __construct(){
        print_r($name.$version.$slug);
    }
}

class Backend_Settings{
    public function __construct(){
        print_r($name.$version.$slug);
    }
}



$firstapp = new application( 'First app', '1.0', 'first-app');
$secondapp = new application( 'Second app', '1.0', 'second-app');

【问题讨论】:

  • 这是一个想法——将应用实例化传递给前端和后端设置:$frontend = new FrontEnd_Settings($this); 然后(假设应用具有 getter 函数),前端可以访问应用程序的所有变量:class Frontend_Settings { public function __construct($app) { printf(“%s.%s.%s”, $app->name(), $app->version(), $app->slug() ); }} 其中名称() 等都是吸气剂。

标签: php oop inheritance


【解决方案1】:

在我的头顶...

<?php

class application{

    protected $name;
    protected $version;
    protected $slug;

    public function __construct(){

        $this->name = $name;
        $this->version = $version;
        $this->slug = $slug;

        // you might want traits instead?
        $this->includes();
    }

    public function create_settings(){
        //Only one instantiation
        $frontend = new Frontend_Settings($this);
        $backend = new Backend_Settings($this);
        //.. more like these
    }

    public function name($name=null) {
      if($name) {
        $this->name = $name;
      }
      return $this->name;
    }

    public function version($version = null) {
      if($version) {
        $this->version = $version;
      }
      return $this->version;
    }

    public function slug($slug = null) {
      if($slug) {
        $this->slug = $slug;
      }
      return $this->slug;
    }

}

class Frontend_Settings{
    public function __construct($app){
        $this->app = $app;
        printf(
          "%s.%s.%s",
          $app->name(),
          $app->version(),
          $app->slug()
        );
    }
}

class Backend_Settings{
    public function __construct($app){
        $this->app = $app;
        printf(
          "%s.%s.%s",
          $app->name(),
          $app->version(),
          $app->slug()
        );
    }
}



$firstapp = new application( 'First app', '1.0', 'first-app');
$secondapp = new application( 'Second app', '1.0', 'second-app');

但请注意,如果 Frontend 或 Backend 对 $app 进行任何更改,该更改将通过引用传递:即,如果您在前端更改它,它会在后端更改。

【讨论】:

  • 非常感谢。这正是我想要的。
猜你喜欢
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多