OOP样式编码

时间:2012-11-12 17:26:43

标签: php

更新 根据专家的指示,我用这种方式修改了代码:

<?php
//Class Validation
class transaction_validate{

private $val_id;
    function __construct($id) {
        $this->val_id = $id;
    }

public function status(){
    //Call our server and decode json value
    $data = $this->get_data($this->val_id);
    $obj = json_decode($data);

    return $obj->status;
}

public function amount(){
    //Call our server and decode json value
    $data = $this->get_data($this->val_id);
    $obj = json_decode($data);

    return $obj->amount;
}


//Create a function for fetching data
private function get_data($sid){
    $url = 'https://domain.com/verify/process.php?trx='.$sid;
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
}

$validate = new transaction_validate();
$validate->val_id = '5201211120944';
echo $validate->amount();
echo '<br>';
echo $validate->status();

输出

  

警告:缺少transaction_validate :: __ construct()的参数1,

     

致命错误:无法访问私有属性transaction_validate :: $ val_id

困惑!

============================上一个问题================= =====================

我只是在学习OOP风格的PHP编码。试图为个人使用编写脚本似乎是错误的。代码是:

<?php
//Class Validation
class transaction_validate{

    var $val_id;

    public function status(){
        //Call our server and decore json value
        $data = get_data();
        $obj = json_decode($data);

        return $obj->status;
    }

    public function amount(){
        //Call our server and decore json value
        $data = get_data();
        $obj = json_decode($data);

        return $obj->amount;
    }


    //Create a function for featching data
    private function get_data($val_id){
        $url = 'https://domain.com/verify/process.php?trx='.$val_id;
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}

$validate = new transaction_validate('5201211120944');
echo $validate->amount();
echo '<br>';
echo $validate->status();

输出

  

致命错误:调用未定义的函数get_data()   第17行/home/userdo/public_html/class.php

我需要认识到我做错了什么。

3 个答案:

答案 0 :(得分:5)

你需要告诉PHP函数在类上下文中:

$this->get_data()

如评论中所述,您应该添加一个构造函数来设置您传入的ID ...

class transaction_validate{

    private $val_id;

    function __construct($id) {
        $this->val_id = $id;
    }

    //...

此外,如果您正在编写OOP,验证可能属于代表事务的域实体,而不是验证助手类。

答案 1 :(得分:1)

这是因为get_data()未在全局范围内定义,但在transaction_validate类中(顺便提一下,我建议将其更改为TransactionValidate;这是其中之一OOP命名约定。)

要调用函数(在OOP - 方法中),您需要使用变量$this,它基本上指向类的当前实例(在本例中为transaction_validate的实例。

所以代码应如下所示:

$data = $this->get_data();

答案 2 :(得分:1)

要调用类中定义的函数,必须使用类实例调用它,即定义函数的类的对象。

在上面的例子中,get_data是一个在类中定义的私有函数,根据OOP的数据隐藏原则,私有成员只能在同一个类中访问 它们隐藏在类之外意味着私有成员不能在类定义之外调用。

因此将以下列方式调用

<?php

   //Class Validation
  class transaction_validate{

var $val_id;

public function status(){
    //Call our server and decore json value
    $data =$this->get_data();
    $obj = json_decode($data);

    return $obj->status;
}

public function amount(){
    //Call our server and decore json value
    $data = $this0get_data();
    $obj = json_decode($data);

    return $obj->amount;
}


//Create a function for featching data
private function get_data($val_id){
    $url = 'https://domain.com/verify/process.php?trx='.$val_id;
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
}

         $validate = new transaction_validate('5201211120944');
         echo $validate->amount();
          echo '<br>';
         echo $validate->status();