传递方法作为构造函数参数

时间:2016-03-07 12:31:19

标签: php oop php-5.3

我正在尝试为API编写一个类,我需要我的构造函数使用一些方法作为参数(因为我将从csv获取数据)..我正在做一些测试:< / p>

class API {

    public $a;
    public $b;

    function __construct(){
        $this->a = setA($a);
        $this->b = setB($b);
    }

    function setA($a){
        $a = "X";
    }

    function setB($b){
        $b = "Y";
    }
}

但它不起作用。这甚至是可能的还是正确的?

编辑按照用户Halcyon的要求。

原始设计是在各种相互作用的功能上进行的。这不是最好的,因为数据被反复提取而不是仅从1个地方读取。

csv和json的方法是:

function getJsonData(){
    $stream = fopen('php://input', 'rb');
    $json = stream_get_contents($stream);
    fclose($stream);
    $order_json_data = json_decode($json, true);
    return $order_json_data;
}

function getProductsTable($csvFile = '', $delimiter = ','){
    if (!file_exists($csvFile) || !is_readable($csvFile))
        echo 'File not here';

    $header = NULL;
    $data = array();

    if (($handle = fopen($csvFile, 'r')) !== FALSE){
        while (($row = fgetcsv($handle, 100, $delimiter)) !== FALSE){
            if (!$header)
                $header = $row;

            else if($row[0] != ''){
                $row = array_merge(array_slice($row,0,2), array_filter(array_slice($row, 2)));
                $sku = $row[0];
                $data[$sku]['productCode'] = $row[1];
                $data[$sku]['Description'] = $row[2];
            }
        }

        fclose($handle);
    }

    array_change_key_case($data, CASE_LOWER);
    return $data;
}

编辑:包括我正在测试对象的索引文件。

<?php
require_once 'helpers/API.php';

if (in_array($_GET['action'],array('insertOrder','updateOrder'))){
    $api = new API();

    file_put_contents('debug/debug_info.txt', "Object response: {$api->a}, {$api->b}", FILE_APPEND | LOCK_EX);
}

3 个答案:

答案 0 :(得分:1)

代码有些问题。以下是一些展示不同方法的示例:

- 示例1

class Foo {

    public $a;
    public $b;

    function __construct(){
        $this->setA();
        $this->setB();
    }

    function setA(){
        $this->a = "X";
    }

    function setB(){
        $this->b = "Y";
    }
}

- 示例2

class Foo {

    public $a;
    public $b;

    function __construct(){
        $this->a = $this->setA();
        $this->b = $this->setB();
    }

    function setA(){
        return "X";
    }

    function setB(){
        return "Y";
    }
}

请注意,您的代码更像第二个示例,但它不起作用,因为函数没有返回任何内容(并且它缺少$this)。

我不知道 $a$b是什么,或者您是否想要在类中设置值(如果它们是常量或类似的话)但是,我想提请你注意第二个例子的一个重要方面 - 特别是如果你真的在设计一个API。在OOP中,我们通常有吸气剂和二传手。当我们封装我们的类时,它们基本上被使用。这是一个封装类的例子:

class Bar{
    private $a;
    public function getA(){ return $this->a; }
    public function setA($a){ $this->a = $a; }
}

请注意$a是私有的,因此我们无法直接访问该类。我们来使用这些方法。这样我们就可以控制该属性的访问权限,进行一些验证等等。(如果设计得很好)这使我们有机会进一步改变获取/设置值的方式的实现,而不必在其中查找它们的出现。整个项目。如果将来您认为$a只能包含数字,只需更改设置器即可。

这实际上取决于$a是什么。您还可以使用__construct初始化此变量。有许多不同的方法可以做同样的事情。无论如何,请看Why use getters and setters?

答案 1 :(得分:0)

您的代码中存在错误:

class API {

 public $a;
 public $b;

 function __construct($a=null, $b=null){
    $this->a = $a;
    $this->b = $b;
 }

 function setA($a){
    $this->a = $a;
 }

 function setB($b){
    $this->b = $b;
 }
}

还引用了对象方法,并避免在函数范围中使用非声明的变量。

$api = new Api("test", "another value" );

答案 2 :(得分:0)

我不明白它为什么需要,但是你可以通过这种方式创建出来:

function __construct(){
    $this->a = [$this, 'setA'];
    $this->b = [$this, 'setB'];
}

现在,您可以使用

$propertyWithCallable = $object->a;
$propertyWithCallable();