0

I am setting up variable values in a constructor function.
To keep everything organised, I have created other classes which will only be instantiated once in the "application" class.
I want to pass protected variables value to other classes(frotend, backend ...). I know we can create same variables in these classes & pass variables as arguments. This will lead to a lot of code repetition.
Is there any better way around?
Thanks

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');
1
  • 1
    Here’s an idea— pass the app instantiation to the frontEnd and backEnd settings: $frontend = new FrontEnd_Settings($this); Then (assuming app has getter functions), frontend has access to all the variables of Application: class Frontend_Settings { public function __construct($app) { printf(“%s.%s.%s”, $app->name(), $app->version(), $app->slug() ); }} where name() et al are getters. Commented Jul 25, 2019 at 4:14

1 Answer 1

1

Off the top of my head...

<?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');

Be aware, though, that if Frontend or Backend makes any changes to $app, that change will be passed by reference: i.e., if you change it in front end, it's changed in back end.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. This is exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.