I have the following:
public function go(){
for($x=0;$x<=31;$x++) {
$this->get_answerrules($x);
$DoneTime+=$DoneTime;
}
echo $gmdate("i:s", $DoneTime);;
}
and
public function get_answerrules($x){
...
...
...
if($response = $this->request($data)){
$obj = json_decode($response,true);
foreach($obj as $file) {
$Time += $file['batch_dura'];
}
$DoneTime = $Time;
return $DoneTime;
}else{}
}
How do I get the value from the 31 for loop cycles and add them together?
Right now my results are coming up blank.
You are not using the results of your method call:
public function go(){
for($x=0;$x<=31;$x++) {
$this->get_answerrules($x);
$DoneTime+=$DoneTime;
}
// The below won't work as `$gmdate` is not in the scope of the method.
echo $gmdate("i:s", $DoneTime);;
}
Should probably be something like:
public function go() {
// Initialize the variable
$DoneTime = 0;
for($x = 0; $x <= 31; $x++) {
$DoneTime += $this->get_answerrules($x);
}
echo $gmdate("i:s", $DoneTime);
}
Related
I have a big foreach where I do many calculations and it looks too messy and bad, that's why I want to break it into more functions but I don't know how since I don't know what arguments should I pass to my sub-functions
Here is my code
$dataset = 'supplier_invoices';
$SupplierInvoice = new Dbm_Supplier($dataset);
$totalValoare = 0;
$costAdaos = 0;
$totalTva_neex = 0;
$totalVal_cu_tva = 0;
$totalValoareCantitateNegativaProduseFacturate = 0;
$costAdaosCantitateNegativaProduseFacturate = 0;
$totalTva_neexCantitateNegativaProduseFacturate = 0;
$totalVal_cu_tvaCantitateNegativaProduseFacturate = 0;
$totalValoarePozitivaProduseFacturate = 0;
$costAdaosPozitivaProduseFacturate = 0;
$totalTva_neexPozitivaProduseFacturate = 0;
$totalVal_cu_tvaPozitivaProduseFacturate = 0;
$totalValoareProduseFurnizor = 0;
$costAdaosProduseFurnizor = 0;
$totalTva_neexProduseFurnizor = 0;
$totalVal_cu_tvaProduseFurnizor = 0;
$totalValoareProduseFurnizorReturnate = 0;
$costAdaosProduseFurnizorReturnate = 0;
$totalTva_neexProduseFurnizorReturnate = 0;
$totalVal_cu_tvaProduseFurnizorReturnate = 0;
foreach ($invoices as $invoice)
{
$data = $SupplierInvoice->findFirst(array('supplier_invoice_id'=>$invoice['supplier_invoice_id']));
$data = $data['results'];
$invoiceCalculus = new InvoiceSupplierCalculus($data['supplier_invoice_date'], $data['currency_rate']);
$products = $SupplierInvoice->getProductsForInvoice($invoice['supplier_invoice_id']);
foreach ($products as $product)
{
$calculus = $invoiceCalculus->getCalculus($product);
$valoare = $calculus['default_currency_value'];
$adaos_com = $calculus['adaos_com'];
$tva_neex = $calculus['tva_neex'];
$val_cu_tva = $calculus['val_cu_tva'];
$totalValoare += $valoare;
$costAdaos += $adaos_com;
$totalTva_neex += $tva_neex;
$totalVal_cu_tva += $val_cu_tva;
$totalVal_cu_tva = number_format($totalVal_cu_tva,2,'.','');
if($product['invoice_quantity'] < 0)
{
$totalValoareCantitateNegativaProduseFacturate += -1 * abs($valoare);
$costAdaosCantitateNegativaProduseFacturate += -1 * abs($adaos_com);
$totalTva_neexCantitateNegativaProduseFacturate += -1 * abs($tva_neex);
$totalVal_cu_tvaCantitateNegativaProduseFacturate += -1 * abs($val_cu_tva);
$totalVal_cu_tvaCantitateNegativaProduseFacturate = number_format($totalVal_cu_tvaCantitateNegativaProduseFacturate,2,'.','');
}
else
{
$totalValoarePozitivaProduseFacturate += abs($valoare);
$costAdaosPozitivaProduseFacturate += abs($adaos_com);
$totalTva_neexPozitivaProduseFacturate += abs($tva_neex);
$totalVal_cu_tvaPozitivaProduseFacturate += abs($val_cu_tva);
$totalVal_cu_tvaPozitivaProduseFacturate = number_format($totalVal_cu_tvaPozitivaProduseFacturate,2,'.','');
}
if($product['product_quantity'] < 0)
{
$totalValoareProduseFurnizorReturnate += $valoare;
$costAdaosProduseFurnizorReturnate += $adaos_com;
$totalTva_neexProduseFurnizorReturnate += $tva_neex;
$totalVal_cu_tvaProduseFurnizorReturnate += $val_cu_tva;
$totalVal_cu_tvaProduseFurnizorReturnate = number_format($totalVal_cu_tvaProduseFurnizorReturnate,2,'.','');
}
else
{
$totalValoareProduseFurnizor += $valoare;
$costAdaosProduseFurnizor += $adaos_com;
$totalTva_neexProduseFurnizor += $tva_neex;
$totalVal_cu_tvaProduseFurnizor += $val_cu_tva;
$totalVal_cu_tvaProduseFurnizor = number_format($totalVal_cu_tvaProduseFurnizor,2,'.','');
}
}
}
This code calculates prices of products listed on invoices. If a product has a minus before its quantity then it means that the product was returned and its price should be subtracted from the final total.
I would encapsulate all of the calculations and accumulated variables in a class, and in the loop just pass the necessary values into a function on the class that handles the logic.
<?php
class InvoiceTotalsAccumulator
{
private $totalValoare = 0;
private $costAdaos = 0;
private $totalTvaNeex = 0;
private $totalValCuTva = 0;
private $totalValoareCantitateNegativaProduseFacturate = 0;
private $costAdaosCantitateNegativaProduseFacturate = 0;
private $totalTvaNeexCantitateNegativaProduseFacturate = 0;
private $totalValCuTvaCantitateNegativaProduseFacturate = 0;
private $totalValoarePozitivaProduseFacturate = 0;
private $costAdaosPozitivaProduseFacturate = 0;
private $totalTvaNeexPozitivaProduseFacturate = 0;
private $totalValCuTvaPozitivaProduseFacturate = 0;
private $totalValoareProduseFurnizor = 0;
private $costAdaosProduseFurnizor = 0;
private $totalTvaNeexProduseFurnizor = 0;
private $totalValCuTvaProduseFurnizor = 0;
private $totalValoareProduseFurnizorReturnate = 0;
private $costAdaosProduseFurnizorReturnate = 0;
private $totalTvaNeexProduseFurnizorReturnate = 0;
private $totalValCuTvaProduseFurnizorReturnate = 0;
public function addProductCalculus($invoiceQuantity, $productQuantity, $valoare, $adaosCom, $tvaNeex, $valCuTva)
{
$this->totalValoare += $valoare;
$this->costAdaos += $adaosCom;
$this->totalTvaNeex += $tvaNeex;
$this->totalValCuTva += $valCuTva;
if ($invoiceQuantity < 0)
{
$this->totalValoareCantitateNegativaProduseFacturate += -1 * abs($valoare);
$this->costAdaosCantitateNegativaProduseFacturate += -1 * abs($adaosCom;
$this->totalTvaNeexCantitateNegativaProduseFacturate += -1 * abs($tvaNeex);
$this->totalValCuTvaCantitateNegativaProduseFacturate += -1 * abs($valCuTva);
}
else
{
$this->totalValoarePozitivaProduseFacturate += abs($valoare);
$this->costAdaosPozitivaProduseFacturate += abs($adaosCom;
$this->totalTva_neexPozitivaProduseFacturate += abs($tvaNeex);
$this->totalValCuTvaPozitivaProduseFacturate += abs($valCuTva);
}
if ($productQuantity < 0)
{
$this->totalValoareProduseFurnizorReturnate += $valoare;
$this->costAdaosProduseFurnizorReturnate += $adaosCom;
$this->totalTva_neexProduseFurnizorReturnate += $tvaNeex;
$this->totalValCuTvaProduseFurnizorReturnate += $valCuTva;
}
else
{
$this->totalValoareProduseFurnizor += $valoare;
$this->costAdaosProduseFurnizor += $adaosCom;
$this->totalTva_neexProduseFurnizor += $tvaNeex;
$this->totalValCuTvaProduseFurnizor += $valCuTva;
}
}
private function _formatNumberForOutput($number)
{
return number_format($number, 2, '.', '');
}
public function getTotalValoare()
{
return $this->totalValoare;
}
public function setTotalValoare(int $totalValoare)
{
$this->totalValoare = $totalValoare;
}
public function getCostAdaos()
{
return $this->costAdaos;
}
public function setCostAdaos(int $costAdaos)
{
$this->costAdaos = $costAdaos;
}
public function getTotalTvaNeex()
{
return $this->totalTvaNeex;
}
public function setTotalTvaNeex(int $totalTvaNeex)
{
$this->totalTvaNeex = $totalTvaNeex;
}
public function getTotalValCuTva()
{
return $this->_formatNumberForOutput($this->totalValCuTva);
}
public function setTotalValCuTva(int $totalValCuTva)
{
$this->totalValCuTva = $totalValCuTva;
}
public function getTotalValoareCantitateNegativaProduseFacturate()
{
return $this->totalValoareCantitateNegativaProduseFacturate;
}
public function setTotalValoareCantitateNegativaProduseFacturate(int $totalValoareCantitateNegativaProduseFacturate)
{
$this->totalValoareCantitateNegativaProduseFacturate = $totalValoareCantitateNegativaProduseFacturate;
}
public function getCostAdaosCantitateNegativaProduseFacturate()
{
return $this->costAdaosCantitateNegativaProduseFacturate;
}
public function setCostAdaosCantitateNegativaProduseFacturate(int $costAdaosCantitateNegativaProduseFacturate)
{
$this->costAdaosCantitateNegativaProduseFacturate = $costAdaosCantitateNegativaProduseFacturate;
}
public function getTotalTvaNeexCantitateNegativaProduseFacturate()
{
return $this->totalTvaNeexCantitateNegativaProduseFacturate;
}
public function setTotalTvaNeexCantitateNegativaProduseFacturate(int $totalTvaNeexCantitateNegativaProduseFacturate)
{
$this->totalTvaNeexCantitateNegativaProduseFacturate = $totalTvaNeexCantitateNegativaProduseFacturate;
}
public function getTotalValCuTvaCantitateNegativaProduseFacturate()
{
return $this->_formatNumberForOutput($this->totalValCuTvaCantitateNegativaProduseFacturate);
}
public function setTotalValCuTvaCantitateNegativaProduseFacturate(int $totalValCuTvaCantitateNegativaProduseFacturate)
{
$this->totalValCuTvaCantitateNegativaProduseFacturate = $totalValCuTvaCantitateNegativaProduseFacturate;
}
public function getTotalValoarePozitivaProduseFacturate()
{
return $this->totalValoarePozitivaProduseFacturate;
}
public function setTotalValoarePozitivaProduseFacturate(int $totalValoarePozitivaProduseFacturate)
{
$this->totalValoarePozitivaProduseFacturate = $totalValoarePozitivaProduseFacturate;
}
public function getCostAdaosPozitivaProduseFacturate()
{
return $this->costAdaosPozitivaProduseFacturate;
}
public function setCostAdaosPozitivaProduseFacturate(int $costAdaosPozitivaProduseFacturate)
{
$this->costAdaosPozitivaProduseFacturate = $costAdaosPozitivaProduseFacturate;
}
public function getTotalTvaNeexPozitivaProduseFacturate()
{
return $this->totalTvaNeexPozitivaProduseFacturate;
}
public function setTotalTvaNeexPozitivaProduseFacturate(int $totalTvaNeexPozitivaProduseFacturate)
{
$this->totalTvaNeexPozitivaProduseFacturate = $totalTvaNeexPozitivaProduseFacturate;
}
public function getTotalValCuTvaPozitivaProduseFacturate()
{
return $this->_formatNumberForOutput($this->totalValCuTvaPozitivaProduseFacturate);
}
public function setTotalValCuTvaPozitivaProduseFacturate(int $totalValCuTvaPozitivaProduseFacturate)
{
$this->totalValCuTvaPozitivaProduseFacturate = $totalValCuTvaPozitivaProduseFacturate;
}
public function getTotalValoareProduseFurnizor()
{
return $this->totalValoareProduseFurnizor;
}
public function setTotalValoareProduseFurnizor(int $totalValoareProduseFurnizor)
{
$this->totalValoareProduseFurnizor = $totalValoareProduseFurnizor;
}
public function getCostAdaosProduseFurnizor()
{
return $this->costAdaosProduseFurnizor;
}
public function setCostAdaosProduseFurnizor(int $costAdaosProduseFurnizor)
{
$this->costAdaosProduseFurnizor = $costAdaosProduseFurnizor;
}
public function getTotalTvaNeexProduseFurnizor()
{
return $this->totalTvaNeexProduseFurnizor;
}
public function setTotalTvaNeexProduseFurnizor(int $totalTvaNeexProduseFurnizor)
{
$this->totalTvaNeexProduseFurnizor = $totalTvaNeexProduseFurnizor;
}
public function getTotalValCuTvaProduseFurnizor()
{
return $this->_formatNumberForOutput($this->totalValCuTvaProduseFurnizor);
}
public function setTotalValCuTvaProduseFurnizor(int $totalValCuTvaProduseFurnizor)
{
$this->totalValCuTvaProduseFurnizor = $totalValCuTvaProduseFurnizor;
}
public function getTotalValoareProduseFurnizorReturnate()
{
return $this->totalValoareProduseFurnizorReturnate;
}
public function setTotalValoareProduseFurnizorReturnate(int $totalValoareProduseFurnizorReturnate)
{
$this->totalValoareProduseFurnizorReturnate = $totalValoareProduseFurnizorReturnate;
}
public function getCostAdaosProduseFurnizorReturnate()
{
return $this->costAdaosProduseFurnizorReturnate;
}
public function setCostAdaosProduseFurnizorReturnate(int $costAdaosProduseFurnizorReturnate)
{
$this->costAdaosProduseFurnizorReturnate = $costAdaosProduseFurnizorReturnate;
}
public function getTotalTvaNeexProduseFurnizorReturnate()
{
return $this->totalTvaNeexProduseFurnizorReturnate;
}
public function setTotalTvaNeexProduseFurnizorReturnate(int $totalTvaNeexProduseFurnizorReturnate)
{
$this->totalTvaNeexProduseFurnizorReturnate = $totalTvaNeexProduseFurnizorReturnate;
}
public function getTotalValCuTvaProduseFurnizorReturnate()
{
return $this->_formatNumberForOutput($this->totalValCuTvaProduseFurnizorReturnate);
}
public function setTotalValCuTvaProduseFurnizorReturnate(int $totalValCuTvaProduseFurnizorReturnate)
{
$this->totalValCuTvaProduseFurnizorReturnate = $totalValCuTvaProduseFurnizorReturnate;
}
public function toArray()
{
return [
'totalValoare' => $this->getTotalValoare(),
'costAdaos' => $this->getCostAdaos(),
'totalTvaNeex' => $this->getTotalTvaNeex(),
'totalValCuTva' => $this->getTotalValCuTva(),
'totalValoareCantitateNegativaProduseFacturate' => $this->getTotalValoareCantitateNegativaProduseFacturate(),
'costAdaosCantitateNegativaProduseFacturate' => $this->getCostAdaosCantitateNegativaProduseFacturate(),
'totalTvaNeexCantitateNegativaProduseFacturate' => $this->getTotalTvaNeexCantitateNegativaProduseFacturate(),
'totalValCuTvaCantitateNegativaProduseFacturate' => $this->getTotalValCuTvaCantitateNegativaProduseFacturate(),
'totalValoarePozitivaProduseFacturate' => $this->getTotalValoarePozitivaProduseFacturate(),
'costAdaosPozitivaProduseFacturate' => $this->getCostAdaosPozitivaProduseFacturate(),
'totalTvaNeexPozitivaProduseFacturate' => $this->getTotalTvaNeexPozitivaProduseFacturate(),
'totalValCuTvaPozitivaProduseFacturate' => $this->getTotalValCuTvaPozitivaProduseFacturate(),
'totalValoareProduseFurnizor' => $this->getTotalValoareProduseFurnizor(),
'costAdaosProduseFurnizor' => $this->getCostAdaosProduseFurnizor(),
'totalTvaNeexProduseFurnizor' => $this->getTotalTvaNeexProduseFurnizor(),
'totalValCuTvaProduseFurnizor' => $this->getTotalValCuTvaProduseFurnizor(),
'totalValoareProduseFurnizorReturnate' => $this->getTotalValoareProduseFurnizorReturnate(),
'costAdaosProduseFurnizorReturnate' => $this->getCostAdaosProduseFurnizorReturnate(),
'totalTvaNeexProduseFurnizorReturnate' => $this->getTotalTvaNeexProduseFurnizorReturnate(),
'totalValCuTvaProduseFurnizorReturnate' => $this->getTotalvalcutvaprodusefurnizorreturnate(),
];
}
}
Then the loop becomes dead simple:
<?php
$dataset = 'supplier_invoices';
$SupplierInvoice = new Dbm_Supplier($dataset);
$accumulator = new InvoiceTotalsAccumulator();
foreach ($invoices as $invoice)
{
$data = $SupplierInvoice->findFirst(['supplier_invoice_id' => $invoice['supplier_invoice_id']]);
$data = $data['results'];
$invoiceCalculus = new InvoiceSupplierCalculus($data['supplier_invoice_date'], $data['currency_rate']);
$products = $SupplierInvoice->getProductsForInvoice($invoice['supplier_invoice_id']);
foreach ($products as $product)
{
$calculus = $invoiceCalculus->getCalculus($product);
$accumulator->addProductCalculus(
$product['invoice_quantity'],
$product['product_quantity'],
$calculus['default_currency_value'],
$calculus['adaos_com'],
$calculus['tva_neex'],
$calculus['val_cu_tva']
);
}
}
If this were my project, I would probably create some sort of DTO class to set the values into and pass an instance of it to the addProductCalculus function instead of the 6 different parameters, but that's probably overkill.
I have these 2 functions below. I want to change $report->rate to count of results. So total rows from this query foreach($campaign->reports->where('status', 2) so if there is 10 results that's true it will be $total_cap = $total_cap + 10;
public static function checkTotalCap(Campaign $campaign)
{
$total_cap = 0;
foreach($campaign->reports->where('status', 2) as $report)
{
$total_cap = $total_cap + $report->rate;
}
if($campaign->cap <= $total_cap)
return true;
return false;
}
public static function checkDailyCap(Campaign $campaign)
{
$daily_cap = 0;
foreach($campaign->reports->where('status', 2) as $report)
{
if($report->created_at->isToday())
{
$daily_cap = $daily_cap + $report->rate;
}
}
if($campaign->daily_cap <= $daily_cap)
return true;
return false;
}
This is how its used
if($campaign->cap !== 0)
{
// Check if total cap or daily cap reached
if($this->checkTotalCap($campaign) || $this->checkDailyCap($campaign))
{
$campaign->active = 'no';
$campaign->save();
return "The campaign has reached its cap.";
}
}
You can do this pretty simply
$reports = $campaign->reports()->where('status', 2)->whereDate('created_at', now())->get();
foreach($reports as $report)
{
$daily_cap = $daily_cap + $report->rate;
}
Write separate query with conditions and simply loop it.
You can try this to filter the reports with status == 2 and:
$reports = $campaign->reports()->where('status', 2)->get();
foreach($reports as $report) {
//TODO
}
You don't even need the foreach() loops at all here:
public static function checkTotalCap(Campaign $campaign) {
return $campaign->reports()->where('status', 2)->count() > $campaign->cap;
// OR
// $campaign->cap <= return $campaign->reports()->where('status', 2)->count();
}
public static function checkDailyCap(Campaign $campaign) {
return $campaign->reports()->where('created_at', Carbon::today())->where('status', 2)->count() > $campaign->daily_cap;
// OR
// return $campaign->daily_cap <= $campaign->reports()->where('created_at', Carbon::today())->where('status', 2)->count();
}
Use SQL-level queries to get the SUM of each Report's rate column, and compare against $campaign->cap.
I'm trying to implement the ball clock (determine how many iterations an inputed number of balls in the initial queue would go before returning to its initial order) in php. I believe at this point my code is not recognizing when to stop. I'm using an SplQueue to represent the queue and keeping an int inside each ball that represents their order.
I believe the difficulty to be in traversing the $queue in the function hasTerminated(). The code runs, it just doesn't stop. An input of 30, for example (php script.php 30) should output 30 12 hour cycles to complete, but it does not terminate. I appreciate any and all ideas as to how to access the objects of the $queue and compare them without destroying the $queue. PHP is not a language I am very comfortable with.
<?php
class Ball {
static $counter = 0;
protected $index;
function __construct(){
$this->index = self::$counter;
self::$counter++;
}
function getIndex(){
return $this->index;
}
}
class Clock {
protected $queue;
protected $minuteTrack;
protected $fiveMinuteTrack;
protected $hourTrack;
protected $count;
protected $time = 0;
function __construct($num){
$this->queue = new \SplQueue;
$this->minuteTrack = new \SplStack;
$this->fiveMinuteTrack = new \SplStack;
$this->hourTrack = new \SplStack;
$this->count = $num;
for($i = 0; $i<$num; $i++){
$ball = new Ball();
$this->queue->enqueue($ball); //inefficient
}
}
function hasTerminated(){
$inOrder = true;
$prev = -1;
$q = clone $this->queue;
while($q->count() != 0){
$elt = $q->dequeue();
if($elt->getIndex() >= $prev){
$inOrder = false;
} else {
$prev = $elt->getIndex();
}
}
echo PHP_EOL;
if($inOrder){
echo 'It takes ' . $time . ' 12 hour cycles to return to the original ordering' . PHP_EOL;
exit(0);
} else {
echo 'Not finished yet' . PHP_EOL;
}
}
function run(){
while(true){
$ball = $this->queue->dequeue();
if(!$this->addBallToMinuteTrack($ball)){
if(!$this->addBallToFiveMinuteTrack($ball)){
if(!$this->addBallToHourTrack($ball)){
$this->queue->enqueue($ball);
}
}
}
}
}
function addBallToMinuteTrack($ball){
echo '1';
if($this->minuteTrack->count() < 4){
$this->minuteTrack->push($ball);
return true;
} else if( $this->minuteTrack->count() == 4){
for($i=0; $i<4; $i++){
$this->queue->enqueue($this->minuteTrack->pop()); //hate this
}
return false;
}
}
function addBallToFiveMinuteTrack($ball){
echo '5';
if($this->fiveMinuteTrack->count() < 11){
$this->fiveMinuteTrack->push($ball);
return true;
} else if( $this->fiveMinuteTrack->count() == 11){
for($i=0; $i<11; $i++){
$this->queue->enqueue($this->fiveMinuteTrack->pop());
}
return false;
}
}
function addBallToHourTrack($ball){
echo '60' . PHP_EOL;
if($this->hourTrack->count() < 11){
$this->hourTrack->push($ball);
return true;
} else if( $this->hourTrack->count() == 11){
for($i=0; $i<11; $i++){
$this->queue->enqueue($this->hourTrack->pop()); //hate this
}
$this->time = $this->time + 1; //In half day increments
$this->hasTerminated();
return false;
}
}
}
foreach (array_slice($argv, 1) as $arg) {
if(!is_numeric($arg)){
echo 'Arguments must be numeric' . PHP_EOL;
exit(1);
} else {
$clock = new Clock($arg);
$clock->run();
}
}
?>
class fruits
{
function g($str = 'fruits'){
$i=0;
$new_str = "";
while ($i < strlen($str)-1){
$new_str = $new_str + $str[$i+1];
$i = $i + 1;
}
return $new_str;
}
function f($str = 'fruits') {
if (strlen($str)== 0) {
return "";
}
else if (strlen($str)== 1)
{
return $str;
}
else
{
return $this->f($this->g($str)) + $str[0]; }
}
function h($n=1, $str = 'fruits'){
while ($n != 1){
if ($n % 2 == 0){
$n = $n/2;
}
else
{
$n = 3*$n + 1;
}
$str = $this->f($str);
}
return $str;
}
function pow($x, $y){
if (y==0)
{
return 1;
}
else
{
return $x * $this->pow($x, $y-1);
}
}
}
$obj = new fruits;
print(h(pow());
I only want to ask how to echo a function like this print(h(pow);?
First turn on error reporting with:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
?>
And you will see (Besides the typos):
Fatal error: Call to undefined function h() in ...
That is because you have a class with methods. So you have to take an instance of your class an call the method from it, e.g.
$obj = new fruits;
echo $obj->h($obj->pow(4, 5));
This is basic OOP PHP. Also I would highly recommed you to use more meaningful function and variable names!
I'm trying to do something like the following:
// assume $f is an arg to the wrapping function
$self = $this;
$func = function() use($f, $ctx, $self){
$self->remove($func, $ctx); // I want $func to be a reference to this anon function
$args = func_get_args();
call_user_func_array($f, $args);
};
Is it possible to reference the function assigned to $func from with the same function?
Try doing
$func = function() use (/*your variables,*/ &$func) {
var_dump($func);
return 1;
};
http://codepad.viper-7.com/cLd3Fu
Yes you can
See this example: http://php.net/manual/en/functions.anonymous.php#105564
Code from example:
<?php
$fib = function($n) use(&$fib) {
if($n == 0 || $n == 1) return 1;
return $fib($n - 1) + $fib($n - 2);
};
echo $fib(2) . "\n"; // 2
$lie = $fib;
$fib = function(){die('error');};//rewrite $fib variable
echo $lie(5); // error because $fib is referenced by closure
?>
Yes, it is possible if you use a variable by reference. For example:
$func = function($i) use(&$func) {
if ($i <= 0) {
return;
}
echo "Countdown: $i.\n";
$func($i - 1);
};
$func(3);