Php 将类对象传递给不同的函数

Php 将类对象传递给不同的函数,php,Php,真的,我在寻找一点关于实现目标的最佳方法的建议。我在纯PHP中工作,没有任何库。我有一个简单的配置类 <?php class Config { //define my constants } 怎么样,创建两个类。一个是配置,另一个是发挥所有魔力的应用程序。 然后有一个脚本文件使用这两个类 应用程序类 class Application { /** @var Config **/ private $config; public function __co

真的,我在寻找一点关于实现目标的最佳方法的建议。我在纯PHP中工作,没有任何库。我有一个简单的配置类

<?php

class Config
{
   //define my constants
}

怎么样,创建两个类。一个是配置,另一个是发挥所有魔力的应用程序。
然后有一个脚本文件使用这两个类

应用程序类
class Application {

    /** @var Config **/
    private $config;

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

    public function execute() {
        $this->transferFile($this->obtainTextFile());
    }

    private function obtainTextFile() {
        //connect to database and write results to text file

        return $textFile;  //path to the generated file
    }  

    private function transferFile($textFile) {
        //return whether the file was successfully SFTP
    }
}
脚本文件

class Application {

    /** @var Config **/
    private $config;

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

    public function execute() {
        $this->transferFile($this->obtainTextFile());
    }

    private function obtainTextFile() {
        //connect to database and write results to text file

        return $textFile;  //path to the generated file
    }  

    private function transferFile($textFile) {
        //return whether the file was successfully SFTP
    }
}
<?php
require __DIR__ . "/../config/Config.php";
require __DIR__ . "/../Application.php";

$config = new Config();
$app = new Application($config);
$app->execute();

你说
不需要它是一个类
,然后你就有了
$textFile=$this->obtainTextFile($config)
如果
execute()
只是一个函数而不是一个类方法,那么
$this
应该指什么?
$this
用于在该实例中执行代码时指代该类的实例。。。。如果你的代码不是类代码,那就没有意义了抱歉,只是测试了一些东西,忘了删除$this有几种方法有些好的有些坏的,更好的方法,把函数放在一个类中(可能把它们分解成IoC)并传递一次,如果你有更多的类,可能会使用注册表模式,将Config类变成静态类或使用facade,或者使用$GLOBALS['config'],或者在函数中定义
global$config