I'm trying to write a class for an API and I need my constructor to use some methods as parameters (because I'll be getting the data from a csv).. I'm doing some tests with this:
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";
}
}
but it's not working. Is this even possible or correct?
Edit: As requested by user Halcyon.
The original design was made on various functions that interacted with each other. It wasn't the best because data was being fetched over and over instead of read from 1 place only.
The methods for the csv and the json are:
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;
}
Edit: Including the index file where I'm testing the object.
<?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);
}