PHPLearner
PHPLearner

Reputation: 56

OOP style coding

Update As per the instruction of experts, I've modified the code this way:

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

And output is

Warning: Missing argument 1 for transaction_validate::__construct(),

Fatal error: Cannot access private property transaction_validate::$val_id

Confused!

============================Previous Question======================================

I'm just learning OOP style PHP coding. Tried to code a script for personal usage which seems wrong. Code is:

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

And output is

Fatal error: Call to undefined function get_data() in /home/userdo/public_html/class.php on line 17

I need to recognize what wrong I did.

Upvotes: 0

Views: 377

Answers (3)

Rubin Porwal
Rubin Porwal

Reputation: 3845

To invoke a function defined in the class it must be invoked using class instance i.e object of class in which function is defined.

In the above example get_data is a private function defined in class and according to principle of data hiding of OOP private members are accessible within the same class only They are hidden outside the class means private members cannot be invoked outside the class definition.

So it will be invoked in following manner

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

Upvotes: 1

wassup
wassup

Reputation: 2503

This is because get_data() is not defined in the global scope but in the transaction_validate class (by the way, I'd recommend to change it to TransactionValidate; this is one of the OOP naming conventions).

To call functions (in OOP – methods), you need to use the variable $this, which basically points to the current instance of a class (in this case – instance of transaction_validate.

So the code should look like this:

$data = $this->get_data();

Upvotes: 1

Fenton
Fenton

Reputation: 250882

You need to tell PHP the function is in the class context:

$this->get_data()

As mentioned in comments, you should add a constructor to set the id you are passing in...

class transaction_validate{

    private $val_id;

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

    //...

Also, if you are writing OOP, validation probably belongs on the domain entity that represents a transaction, rather than in a validation helper class.

Upvotes: 5

Related Questions