I have a class, that has a method call to an api resource, and other methods that use the api call method output. As it is right now, every time some other method calls the api method, the api method makes a request to the api over and over again. What would be the best way to make a single call to the api and then use the output trough my class? See example.
class foo {
$param1;
$param2;
function getApi {
return 'call_to_api' . $this->param1 . $this->$param2;
}
function do_stuff_1 {
return 'do_some_other_stuff . '$this->getApi() . $param1
}
function do_stuff_2 {
return 'do_some_other_other_stuff . '$this->getApi() . $param2
}
}
You can use Laravel Cache in your requests to the API:
$url = 'http://api.url.com?data1=x&data2=y';
if (Cache::has($url))
{
$apiResult = Cache::get($url);
}
else
{
$apiResult = $this->apiGetResult($url);
Cache::put($url, $apiResult, 5); // cache for 5 minutes
}
return $apiResult;
So, your API will only be hit if the it was never hit before or if the cache expired. The nice thing of using Laravel cache is that it works between requests, so if your application needs that same data in the next request it will not hit the API again.
You can create sort of a cache by calling the API again only if the parameters have been modified:
class foo {
$param1;
$param2;
private $resultAPI = '';
private $paramModified = false;
function getApi {
if ($this->resultAPI == '' || $this->paramModified) {
$this->resultAPI = 'call_to_api' . $this->param1 . $this->$param2;
$this->paramModified = false;
}
return $this->resultAPI;
}
function setParamX($val) {
if ($this->paramX != $val) {
$this->paramX = $val;
$this->paramModified = true;
}
}
function do_stuff_1 {
return 'do_some_other_stuff . '$this->resultAPI . $param1
}
function do_stuff_2 {
return 'do_some_other_other_stuff . '$this->resultAPI . $param2
}
}
Related
Say I have to similar function :
public function auth(){
return $someResponse;
}
public function collect(){
return $someOtherResponse
}
Question : When one of the response get passed to another class, is there any way to check which function returned the response ?
In a purely object-oriented way, wanting to attach information to a value is akin to wrapping it into a container possessing context information, such as:
class ValueWithContext {
private $value;
private $context;
public function __construct($value, $context) {
$this->value = $value;
$this->context = $context;
}
public value() {
return $this->value;
}
public context() {
return $this->context;
}
}
You can use it like this:
function auth()
{
return new ValueWithContext($someresponse, "auth");
}
function collect()
{
return new ValueWithContext($someotherrpesonse, "collect");
}
This forces you to be explicit about the context attached to the value, which has the benefit of protecting you from accidental renamings of the functions themselves.
As per my comment, using arrays in the return will give you a viable solution to this.
It will allow a way to see what has been done;
function auth()
{
return (array("auth" => $someresponse));
}
function collect()
{
return (array("collect" => $someotherrpesonse));
}
class myClass
{
function doSomething($type)
{
if (function_exists($type))
{
$result = $type();
if (isset($result['auth']))
{
// Auth Used
$auth_result = $result['auth'];
}
else if (isset($result['collect']))
{
// Collect used
$collect_result = $result['collect'];
}
}
}
}
It can also give you a way to fail by having a return array("fail" => "fail reason")
As comments say also, you can just check based on function name;
class myClass
{
function doSomething($type)
{
switch ($type)
{
case "auth" :
{
$result = auth();
break;
}
case "collect" :
{
$result = collect();
break;
}
default :
{
// Some error occurred?
}
}
}
}
Either way works and is perfectly valid!
Letting the two user defined functions auth() & collect() call a common function which makes a call to debug_backtrace() function should do the trick.
function setBackTrace(){
$backTraceData = debug_backtrace();
$traceObject = array_reduce($backTraceData, function ($str, $val2) {
if (trim($str) === "") {
return $val2['function'];
}
return $str . " -> " . $val2['function'];
});
return $traceObject;
}
function getfunctionDo1(){
return setBackTrace();
}
function getfunctionDo2(){
return setBackTrace();
}
class DoSomething {
static function callfunctionTodo($type){
return (($type === 1) ? getfunctionDo1() : getfunctionDo2());
}
}
echo DoSomething::callfunctionTodo(1);
echo "<br/>";
echo DoSomething::callfunctionTodo(2);
/*Output
setBackTrace -> getfunctionDo1 -> callfunctionTodo
setBackTrace -> getfunctionDo2 -> callfunctionTodo
*/
The above function would output the which function returned the response
I would like to make the following function available throughout my routers.
public function getAll($request, $response, $args, $table, $prefix, $order, $PermRead) {
// retrieve all records
// WORKING... Security questions
// 1. First, check to make sure authenticated (via JSESSION_ID, etc.)
// 2. Automatically apply site_id to ALL queries
// 3. Apply sch_id to this query
// 4. Get permissions
$status = null;
$site_id = $sch_id = 1;
if (!$PermRead) {
$status = 403; // 403 Forbidden
} else {
$sql =
"SELECT * from " . $table .
" WHERE " . $prefix . "_site_id = " . $site_id .
" AND " . $prefix . "_sch_id = " . $sch_id .
" AND " . $prefix . "_deleted_timestamp IS NULL " .
" ORDER BY " . $order;
$rows = $this->dbw->run($sql);
}
if (!$status) {
$status = 200; // 200 OK
}
return $response->withStatus($status)->withJson($rows);
}
However, I get the following error: Fatal error: Using $this when not in object context in C:\Wamp\www\ravine\server\src\routes.php on line 26
How should I make this function available so that I can call it inside my route, like this:
// retrieve all classroom records
$app->get('/classrooms', function ($request, $response, $args) {
$PermRead = true; // check permissions
return getAll($request, $response, $args, "classroom", "room", "room_name", $PermRead);
});
Implementation of Werner's suggestion to use the application's container:
I created a class called Common in /lib/common.php:
<?php
namespace lib;
use Interop\Container\ContainerInterface;
class Common {
protected $ci;
private $site_id = 1;
//Constructor
public function __construct(ContainerInterface $ci) {
$this->ci = $ci;
}
public function getAll($table, $prefix, $order, $PermRead) {
// retrieve all records
// WORKING... Security questions
// 1. First, check to make sure authenticated (via JSESSION_ID, etc.)
// 2. Automatically apply site_id to ALL queries
// 3. Apply sch_id to this query
// 4. Get permissions
$status = null;
$site_id = $sch_id = 1;
if (!$PermRead) {
$status = 403; // 403 Forbidden
} else {
$sql =
"SELECT * from " . $table .
" WHERE " . $prefix . "_site_id = " . $site_id .
" ORDER BY " . $order;
$rows = $this->ci->dbh->run($sql);
$this->ci->response->withJson($rows);
}
if (!$status) {
$status = 200; // 200 OK
}
return $this->ci->response->withStatus($status);
}
}
Then, I added the class to /src/dependencies.php
<?php
require __DIR__ . '/../lib/common.php';
$container = $app->getContainer();
// common router functions
$container['common'] = function ($c) {
$common = new lib\Common($c);
return $common;
};
Now, within my individual router files, I'm able to call the common function like this in /routers/classroom.router.php:
// retrieve all classroom records
$app->get('/classrooms', function ($request, $response, $args) {
$PermRead = true; // check permissions
return $this->common->getAll("classroom", "room", "room_name", $PermRead);
});
The container carries $request, $response and $args (and other functions).
I would suggest making use of application containers to simplify your application structure. Slim 3 has been designed to work well with application containers.
Pass the container to your class method - you will then have the request and response objects available via the (shared) container, since Slim assigns those (request and response) to the container object automatically.
You can even add/assign your database connection (and whatever else you want to make available to other classes) to the container, then you only need to pass the same container to all functions that require database functionality.
The idea is that you can write classes that can be re-used in other projects, even if you decide to use something different than Slim next time. As long as the framework uses application containers, you can probably re-use your classes.
Eg: In you index.php
$container = $app->getContainer();
$container['db'] = $myDbConnection;
$container['request'] and $container['response'] are assigned automatically by the framework.
E.g MyClass.php
use Interop\Container\ContainerInterface;
class MyClass {
public function getAll(ContainerInterface $container) {
// ...
$myDb = $container['db'];
// ... do DB stuff
$response = $container['response'];
return $response->withStatus($status)->withJson($rows);
}
}
$this is not available in your function, the easiest way would be to just add it as an parameter.
Something like:
public function getAll($request, $response, $args, $table, $prefix, $order, $PermRead, $app) {
[..]
$app->dbw->...;
Then call it with $this in the parameter
return getAll($request, $response, $args, "classroom", "room", "room_name", $PermRead, $this);
I'm trying to implement Simple MVC Framework http://simplemvcframework.com/ and am going through the code line by line in the index.php file (https://github.com/simple-mvc-framework/v2/blob/master/index.php) and I've come across the following 2 lines..
//define routes
Router::any('', '\controllers\welcome#index');
Router::any('/subpage', '\controllers\welcome#subpage');
I understand :: is a scope resolution operator, and would think that Router::any() would be referencing a static method called any() in the Router class... however no such method exists... https://github.com/simple-mvc-framework/v2/blob/master/app/core/router.php. Though all other static method calls mentioned in the index.php file DO exist.
I thought maybe this was some sort of reserved name of a PHP function, but of course as you could imagine, searching for "PHP Any function" or similar searches in google doesn't come back with too many helpful results. My other thought is maybe this is just a implementation of static calls that I'm not familiar with?
I know this is a very specific question, but I'm trying to make sure I understand as much as possible with this framework and PHP in general before going too much futher.
Here is how it works.
It utilizes __callStatic magic method in PHP.
When a static call is made using Class/Object, and if the magic method is defined in the class, and if the called static function doesn't exist then this method is invoked.
If we dig deeper into the code,
public static function __callstatic($method, $params){
$uri = dirname($_SERVER['PHP_SELF']).'/'.$params[0];
$callback = $params[1];
array_push(self::$routes, $uri);
array_push(self::$methods, strtoupper($method));
array_push(self::$callbacks, $callback);
}
The method parameter which is any in our case is store as uppercase (ANY) with a callback.
When a request is made, dispatch function is called.
public static function dispatch(){
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$method = $_SERVER['REQUEST_METHOD'];
$searches = array_keys(static::$patterns);
$replaces = array_values(static::$patterns);
self::$routes = str_replace('//','/',self::$routes);
$found_route = false;
// parse query parameters
{
$query = '';
$q_arr = array();
if(strpos($uri, '&') > 0) {
$query = substr($uri, strpos($uri, '&') + 1);
$uri = substr($uri, 0, strpos($uri, '&'));
$q_arr = explode('&', $query);
foreach($q_arr as $q) {
$qobj = explode('=', $q);
$q_arr[] = array($qobj[0] => $qobj[1]);
if(!isset($_GET[$qobj[0]]))
{
$_GET[$qobj[0]] = $qobj[1];
}
}
}
}
// check if route is defined without regex
if (in_array($uri, self::$routes)) {
$route_pos = array_keys(self::$routes, $uri);
// foreach route position
foreach ($route_pos as $route) {
if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') {
$found_route = true;
//if route is not an object
if(!is_object(self::$callbacks[$route])){
//call object controller and method
self::invokeObject(self::$callbacks[$route]);
if (self::$halts) return;
} else {
//call closure
call_user_func(self::$callbacks[$route]);
if (self::$halts) return;
}
}
}
// end foreach
} else {
// check if defined with regex
$pos = 0;
// foreach routes
foreach (self::$routes as $route) {
$route = str_replace('//','/',$route);
if (strpos($route, ':') !== false) {
$route = str_replace($searches, $replaces, $route);
}
if (preg_match('#^' . $route . '$#', $uri, $matched)) {
if (self::$methods[$pos] == $method || self::$methods[$pos] == 'ANY') {
$found_route = true;
//remove $matched[0] as [1] is the first parameter.
array_shift($matched);
if(!is_object(self::$callbacks[$pos])){
//call object controller and method
self::invokeObject(self::$callbacks[$pos],$matched);
if (self::$halts) return;
} else {
//call closure
call_user_func_array(self::$callbacks[$pos], $matched);
if (self::$halts) return;
}
}
}
$pos++;
}
// end foreach
}
if (self::$fallback) {
//call the auto dispatch method
$found_route = self::autoDispatch();
}
// run the error callback if the route was not found
if (!$found_route) {
if (!self::$error_callback) {
self::$error_callback = function() {
header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found");
echo '404';
};
}
if(!is_object(self::$error_callback)){
//call object controller and method
self::invokeObject(self::$error_callback,null,'No routes found.');
if (self::$halts) return;
} else {
call_user_func(self::$error_callback);
if (self::$halts) return;
}
}
}
}
If you look deeply into the dispatch function, you will clearly see that, there are several lines containing:
if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY')
This helps routing the request to defined callbacks based on the methods supplied including ANY method.
It's not that any() is a reserved method, it's that the class is using overloading to call that method. Look at this code for a second
/**
* Defines a route w/ callback and method
*
* #param string $method
* #param array #params
*/
public static function __callstatic($method, $params){
$uri = dirname($_SERVER['PHP_SELF']).'/'.$params[0];
$callback = $params[1];
array_push(self::$routes, $uri);
array_push(self::$methods, strtoupper($method));
array_push(self::$callbacks, $callback);
}
When any() is called, PHP first checks for that method being defined directly. Since it's not, it then calls this overloading magic method, which then executes the call.
How can i pass a class as a parameter in my function
So far i've tried
$sc = new SampleClass();
SampleFunction($sc);
function SampleFunction(&$refClass)
{
echo $refClass->getValue();
}
this is a simplified example of what im doing.. i actually have to do complex procedures inside this sample function. I'm not getting any response from the sample function. What am i doing wrong? thank you
UPDATE
char.php
class Charss {
var $name=0;
var $hp=500;
var $spd=10;
var $rtime=10;
var $dmg=10;
function __construct( $name, $hp, $spd, $rtime , $dmg) {
$this->name = $name;
$this->hp = $hp;
$this->spd = $spd;
$this->rtime = $rtime;
$this->dmg = $dmg;
}
function get_name() {
return $this->name;
}
function set_name($new_name) {
$this->name = $new_name;
}
function get_hp() {
return $this->hp;
}
function set_hp($new_hp) {
$this->hp = $new_hp;
}
function get_spd() {
return $this->spd;
}
function set_spd($new_spd) {
$this->spd = $new_spd;
}
function get_rtime() {
return $this->rtime;
}
function set_rtime($new_rtime) {
$this->rtime = $new_rtime;
}
function get_dmg() {
return $this->get_dmg;
}
function set_dmg($new_dmg) {
$this->dmg = $new_dmg;
}
}
myclass.php
require("char.php");
class Person {
function try_process()
{
$chr1 = new Charss("Player1",500,3,0,50);
$chr2 = new Charss("Player2",500,6,0,70);
while ($chr1->get_hp() > 0 && $chr2->get_hp() > 0)
{
$sth = min($chr1->get_rtime(), $chr2->get_rtime());
if ($chr1->get_rtime() == 0 && $chr2->get_rtime() > 0)
{
exit;
Fight($chr1,$chr2);
$chr1->set_rtime($chr1->get_spd());
}
elseif ($chr2->get_rtime() == 0 && $chr1->get_rtime() > 0)
{
Fight($chr2,$chr1);
$chr2->set_rtime($chr2->get_spd());
}
else
{
Fight($chr1,$chr2); #having trouble with this
$chr1->set_rtime($chr1->get_spd());
}
$chr1->set_rtime($chr1->get_rtime() - $sth);
$chr2->set_rtime($chr2->get_rtime() - $sth);
}
}
function Fight($atk,$def)
{
$def->set_hp($def->get_hp() - $atk->get_dmg());
echo $atk->get_name() . " attacked " . $def->get_name() . " for " . $atk->get_dmg() . " damage";
}
}
so im calling the function try_process on button click
What you're actually doing there is passing an object, not a class.
$sc = new SampleClass();
creates an instance of SampleClass, aka an object.
I assume there's some error being thrown elsewhere as what you have is correct.
I tested the following code and got the expected output:
class SampleClass
{
public function getValue()
{
return 4;
}
}
$sc = new SampleClass();
SampleFunction($sc);
function SampleFunction(&$refClass)
{
echo $refClass->getValue();
}
Output: 4
If you provide more details of your actual code we might be able to determine the problem.
I can't see anything wrong with your code
using &$refClass is however is not recommended and I guess willbe removed from future iteration of PHP version
but here is an example
class objects are passed as reference I suppose so no need of '&'
http://ideone.com/GbmUy
Why is the function argument a reference? Probably shouldn't be.
Other than that, there's nothing wrong with you posted, so the error is likely within SampleClass.
Others have answered pretty well, but this is a silly little example to show you how to modify the class (either by calling a property setter, or setting public properties directly)
class foo {
private $member1;
public $member2;
public function __construct($member1,$member2) {
$this->member1=$member1;
$this->member2=$member2;
}
public function SetMember1($value) {
$this->member1 = $value;
}
public function GetMember1() {
return $this->member1;
}
}
function SetMembers(foo $obj, $member1, $member2) {
// Call a setter
$obj->SetMember1($member1);
// Set a member variable directly
$obj->member2 = $member2;
}
$obj = new foo('default member 1', 'default member 2');
echo "member1 (before): {$obj->GetMember1()}\n";
echo "member2 (before): {$obj->member2}\n";
// Change values
SetMembers($obj, 'new member1', 'new member2');
echo "member1 (after): {$obj->GetMember1()}\n";
echo "member2 (after): {$obj->member2}\n";
This will output:
member1 (before): default member 1
member2 (before): default member 2
member1 (after): new member1
member2 (after): new member2
I'm very new to PHP and OOP in general. I'm using codeigniter for a framework, and am currently attempting to build a class 'BuildLinks' that will redirect the user to the correct link based on what URL they landed on.
The controller passes the right variables to the class, while the function build_afflink() selects what class to call next based on the var1
The controller:
function out($var1, $var2)
{
// redirect to link after parsing data
$debug='1';
$params = array('var1'=>$var1, 'var2'=>$var2);
$this->load->library('BuildLinks', $params);
if ($debug=='0'){
$redirect = $this->buildlinks->build_afflink();
redirect($redirect, 'location', 301);
}
else {
var_dump($this->buildlinks->build_afflink());
}
}
The class BuildLinks is a work in progress... but it is extended by all of the other sites I need to support.
BuildLinks class:
class BuildLinks
{
public $var1;
public $var2;
public $link;
function __construct($params)
{
//populate up inititial variables from $params array (passed from controller)
$this->var1 = (string)$params['var1'];
$this->var2 = (string)$params['var2'];
echo __class__ . ' loaded....' . 'var1: '.$this->var1 . ' var2: ' .$this->var2. '<br/>';
}
public function get_var1()
{
return $this->var1;
}
public function set_var1($var1)
{
$this->var1 = $var1;
}
public function get_var2()
{
return $this->var2;
}
public function set_var2($var2)
{
$this->var2 = $var2;
}
function build_thelink()
{
switch ($this->var1) {
case 'amazon':
//echo 'Amazon is our vendor.<br>';
$newobj = new Amazon;
// Amazon subclass returns the correct affiliate link
return $newobj->affiliate_link();
break;
case 'ebay':
$newobj = new Ebay;
//ebay subclass however, cannot access the public var var1, it returns a null value for $this->var1
return $newobj->affiliate_link();
break;
}
}
}
Essentially, when I make a new Ebay object, it can't access any of the public variables from the parent class BuildLinks. What am I doing wrong?
In this example, I have it construct the initial variables and echo back out some information for debugging.
EDIT: If I change the __construct to read:
function __construct($params)
{
//populate up inititial variables from $params array (passed from controller)
$this->var1 = (string)$params['var1'];
$this->var2 = (string)$params['var2'];
echo __class__ . ' loaded....' . 'var1: '.$this->var1. ' var2: ' .$this->var2 . '<br/>';
var_dump(get_class_vars(get_class($this)));
}
Then I get this as an output:
BuildLinks loaded....var1: ebay var2: somedata
array
'var1' => null
'var2' => null
'link' => null
The following works fine:
$newobj = new Amazon;
return $newobj->affiliate_link();
This does not, but the classes are almost identical...
$newobj = new Ebay;
return $newobj->affiliate_link();
Here is the ebay class:
class Ebay extends BuildLinks
{
private $res;
//TODO: language/multiple site support
//public $locale;
function __construct()
{
//echo __class__ . ' loaded....' . 'vendor: '.$this->vendor . ' isbn: ' .$this->isbn . '<br/>';
}
function add_to_cart_button($isbn, $locale)
{
}
function affiliate_link()
{
$this->get_info();
return $this->link;
}
// Load $this->api_call() and return Amazon SimpleXML response object, load variables
function get_info()
{
$apicall = $this->api_call();
//build variables
foreach ($apicall->searchResult->item as $item) {
$this->link = (string )$item->viewItemURL;
}
}
// Generate API call and return simplexml object $res
function api_call()
{
//build $apicall here
$res = simplexml_load_file($apicall);
// Check to see if the request was successful, else print an error
if ($res->ack == "Success") {
$this->res = $res;
return $res;
} else {
echo 'api_call() unsuccessful';
}
}
}
This part of the ebay class doesn't make too much sense to me:
function api_call()
{
//build $apicall here
$res = simplexml_load_file($apicall);
// Check to see if the request was successful, else print an error
if ($res->ack == "Success") {
$this->res = $res;
return $res;
} else {
echo 'api_call() unsuccessful';
}
}
simplexml_load_file($apicall); specifically isn't loading anything. Where's $apicall? If you debug that, does it echo "api_call() unsuccessful"?
Because if that's unsuccessful then Ebay::link won't be set and as a result return $newobj->affiliate_link(); wouldn't end up returning anything.
for clarification, $apicall should be set to a file path before calling simplexml_load_file($apicall);
Ebay::get_info() does this
foreach ($apicall->searchResult->item as $item) {
$this->link = (string )$item->viewItemURL;
}
If $apicall->searchResult->item happens to have several elements, last of which is empty, you'll get only that last empty element.
That's a wild guess though.
[added]
You're overwriting a constructor in Ebay, so if you want BuildLinks' class constructor to be called, you need to do this explicitly.