Thread Wrapper Class for a Function with variable arguments in PHP - php

The idea here is to make a class that constructs with a function and an array of parameters and calls that function in a new thread.
This is my class so far:
class FunctionThread extends Thread {
public function __construct($fFunction, $aParameters){
$this->fFunction = $fFunction;
$this->aParameters = $aParameters;
}
public function run(){
$this->fFunction($this->aParmeters[0], $this->aParmeters[1], ...);
}
}
Obviously the run function is incorrect, which brings me to my question:
Assuming the array is guaranteed to have the proper number of elements to match the function that is being called, How can I call a function in PHP with an unknown number of arguments that are stored in an array?
Edit:
Also I have no access to the contents of the given function so it cannot be edited.
Edit 2: I'm looking for something similar to scheme's curry function.

As of PHP 5.6, this is now possible. Arrays can be expanded into the argument list using the ... operator like so:
<?
class FunctionThread extends Thread {
public function __construct($fFunction, $aParameters){
$this->fFunction = $fFunction;
$this->aParameters = $aParameters;
}
public function run(){
$this->fFunction(... $this->aParmeters);
}
}
?>
See here for more information

I think that function should accept in its case the arg array
class FunctionThread extends Thread {
public function __construct($fFunction, $aParameters){
$this->fFunction = $fFunction;
$this->aParameters = $aParameters;
}
public function run(){
$this->fFunction($this->aParmeters);
}
public function fFunction($arr){
$var0 = $arr[0];
$var1 = $arr[1];
...
do
..
}
}

Related

Calling Static Member Function on Class Stored as String in PHP?

Since I am aware that one can call a member function on Class stored as String. But I was wondering if there is any way to do the same for static member function in PHP.
for example:
class A
{
public static function run(){
echo "OK";
}
}
"A"::"run"()
Something similar to the above example.
Please help me out. Thanks in advance.
you can call class as string like this
<?php
class A
{
public static function run()
{
echo "OK";
}
}
$stringClass = "A";
$staticMethod = "run";
$stringClass::$staticMethod();
To tackle this type of problem, which I faced while developing a user-friendly PHP framework, is to store the class name and static method name as a string in some PHP variables.
$className = "A";
$methodName = "run";
And call it like so:
$className::$methodName();
or second way
$func = "A::run";
$func(); //or
"A::run"();
And this is how one calls the static method on the class stored as a string.
This one of the ways, more can be found at https://www.designcise.com/web/tutorial/how-to-dynamically-invoke-a-class-method-in-php

Access an array of a class in another class without passing arguments in php

$mile= new mile();
$get_sales_totals1=mysql_query("SELECT title,deadline FROM milestones where id=$id ");
while($milestone = mysql_fetch_array($get_sales_totals1)){
$mile->addne($milestone["deadline"],$milestone["title"]);
}
I am trying to create an array of objects from database query.Above code is the main.php page and below is the page with classes.I created an array with deadline and title in each item of the array.
class mile {
public $milearray;
public function __construct(){
$this->milearray=array();
}
public function addne($deadline,$title){
$ne->deadline=$deadline;
$ne->title=$title;
array_push($this->milearray,$ne);
}
public function extra(){
//how to get milearray $this->milearray here
}
}
Is it possible to get milearray in the function 'extra' in the same class without passing arguments.
OR
class compare is incomplete. I need to call milearray from class 'mile' inside class 'compare' without passing any arguments
class compare{
public daycomparearray;
public function __construct(){
$this->daycomparearray=array();
}
public function comparemile(){
if($this->daycomparearray== milearrray->deadline)
// how to get mile array here
}
}
please help me....
You have to pass arguments, otherwise the compare class won't know which objects to compare.
class compare{
public function __construct(){
}
public function comparemile($mile1, $mile2){
$array1 = $mile1->milearray;
$array2 = $mile2->milearray;
// code that compares $array1 and $array2
}
}
If you can't pass arguments to the comparemile function, maybe it can be done with the constructor.
class compare{
private $array1;
private $array2;
public function __construct($mile1, $mile2){
$this->array1 = $mile1->milearray;
$this->array2 = $mile2->milearray;
}
public function comparemile(){
// code that compares $this->$array1 and $this->$array2
}
}
You tried self? Like:
public function extra(){
$array = self::$milearray;
}
or making a session for global vars?

Return array variable in PHP

I have a function and I'd like to return a variable to another function.
Can I return the array variable so I can use the variable at other function?
public function update_mdr_pameran() {
//global $araydatamdr;
$this->config->set_item('compress_output', FALSE);
$araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
return $araydatamdr;
}
When I try to use $araydatamdr in another function, it became 0.
Am I missing something?
You can achieve this by calling function and setting its return value to another variable.
Method 1 :
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function update_mdr_pameran() {
//global $araydatamdr;
$this->config->set_item('compress_output', FALSE);
$araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
return $araydatamdr;
}
public function test_func() {
$araydatamdr = $this->update_mdr_pameran();
var_dump($araydatamdr);
}
}
Or you can also set $araydatamdr to $this reference.
Method 2 :
class Test extends CI_Controller {
public $araydatamdr;
public function __construct() {
parent::__construct();
$this->araydatamdr = [];
}
public function update_mdr_pameran() {
$this->config->set_item('compress_output', FALSE);
$this->araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$this->araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$this->araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
}
public function test_func() {
$this->update_mdr_pameran();
var_dump($this->araydatamdr);
}
}
Cross out the echo $araydatamdr; Arrays can be printed using var_dump or print_r. Also you can return an array in php but personally i prefer to json_encode it first so i return a json as the output of my function something like:
return json_encode($araydatamdr);
Then it's a simple function call.
I don't know your project structure but i am giving general guidance. Apart from that i don't see anything else that could block your function.
I edit my post because i saw the issue is to call the function. There are 2 ways depending where you call it. If the function is in the same class as the other function you want to call it you simple go for :
$result=$this->update_mdr_pameran();
I see that your function has no arguments so you don't need to set any. If it's in another file:
1) include your php file at top like :
require 'myphpclass.php';
*tip make sure your path is right.
2) Create a new class object and then call the function like :
$class= new myClass();
$result=$class->update_mdr_pameran();

Convert static method to lambda in PHP

I want to get static method from class and copy it to variable.
This is non-working example illustrating my question:
class foo
{
public static function bar($argument){ return 2*$argument; }
}
$class = new ReflectionClass('foo');
// here is no ReflectionMethod::getClosure() method in reality
$lambda = $class->getMethod('bar')->getClosure();
echo $lambda(3);
So my question: is this possible by any normal way? I find only one way for now. I can parse source file, get method source from it and convert it using create_function() but it's too perverse.
Just wrap it with closure.
$lamda = function($argument){return foo::bar($argument);};
Or you can try to use something like this
function staticMethodToClosure($class, $method) {
return function($argument)use($class, $method){return $class::$method($argument);};
}
An array in the format array($className, $methodName) is invokable as a static method call so this may work for you.
class foo
{
public static function bar($argument){ return 2*$argument; }
public static function getStaticFunction($arg){
return array("foo", $arg);
}
}
$a = foo::getStaticFunction("bar");
echo $a(5); // echos 10

Why won't the shuffle function work in a PHP class?

Why won't it shuffle the array so I get a random result each time?
class greeting {
public $greet = array('hi','hello');
shuffle($greet);
}
$hi = new greeting;
echo $hi->greet[1];
Is their something wrong with my code?
If you change it so the shuffle is inside the constructor it should work fine.
class greeting {
public $greet = array('hi','hello');
function __construct(){
shuffle($this->greet);
}
}
any calculation can not be executed outside the method, inside class.
class greeting {
public $greet = array('hi','hello');
function __construct()
{
shuffle($this->greet);
}
}
$hi = new greeting;
echo $hi->greet[1];
Inside a class block you can only define constants, properties (both with fixed values) and methods. You can't put code in that block, code can only be placed inside methods (AKA functions).

Categories