开发者

How to load a class inside the function in PHP?

开发者 https://www.devze.com 2023-03-08 02:38 出处:网络
I have a class already load inside __construct() of my page, aft开发者_如何学编程er that once again I want to load this class, inside another function. Here is my code.

I have a class already load inside __construct() of my page, aft开发者_如何学编程er that once again I want to load this class, inside another function. Here is my code.

<?php

class MainClass {
  public $config;

  public function __construct($config) {
    $this->config = $config;
  }

  public function routes() {
    // here I need to load the config class
  }
}
?>

inside routes function i want to call an array form config class file


Your $config is already loaded as a property of the object, since you passed it in the constructor. If you need to access the $config from another member function, just use $this->config->whatever

public function routes() {
  //here i need to load the config class
  $something = $this->config->somearray[0];
}


public function routes() {
    require_once "/path/to/YourClass.php"
    $your_class_instance = new YourClass();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号