-1

Similar questions exists - I've tried already some solutions from StackOverflow and Google but still no luck - I can't catch a right syntax or right understanding - so let me repeat my question with more details for clarity.

I have a PHP (PHP 7.4) class file what is a part of MVC with two functions inside- and I try to pass a variable from one function to another using global variable.

My question is - please, check my code below - if you see something wrong then pls give an answer how it should be (because if code is correct with syntax and logic - it means the problem is not inside this file but may be somewhere outside) - but I need other opinions to be sure.

// I use control files for quick tests because if OK I see output immediately
public function indexAction()
{
global $myArr;
if ($this->req->get('lang')) {
$lang = $this->req->get('lang');
$myArr[] = $lang;
file_put_contents('/var/www/html/app/controllers/control_indexaction_lang.php', $lang); // output OK - en de etc...
file_put_contents('/var/www/html/app/controllers/control_indexaction_myarr.php', $myArr); // output OK - en de etc...
// with every change of layout control file changes accordingly to en de it etc
// so $lang variable actually works here
 } else {
$lang = 'en';
}
}
public function searchAction()
{
global $myArr;
file_put_contents('/var/www/html/app/controllers/control_searchaction_lang.php', $lang); // no output
file_put_contents('/var/www/html/app/controllers/control_searchaction_myarr.php', $myArr); // no output
// other code of function works OK
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Serge
  • 679
  • 1
  • 9
  • 23
  • 1
    Since you have a class you can just use a property which is “global” to the class – Chris Haas Jul 02 '22 at 14:16
  • Thx but I have no much experience so I'd need an exact example to avoid syntax errors – Serge Jul 02 '22 at 14:31
  • https://3v4l.org/WbrnG – Chris Haas Jul 02 '22 at 14:48
  • You can't just "write" an array to a file as well. You'll have to use `json_encode` or `var_export` – DarkBee Jul 02 '22 at 15:13
  • Pls, see my comment under first answer, thank you for attention. Also the problem is I really need answers where used exactly my code - what line change to what - sorry my lack of experience, I'm just aftraid to finally confuse the rest of my mind :) – Serge Jul 02 '22 at 15:17
  • Your two methods also write to two different folders, one has `alt` in the path, not sure if it matters. – Chris Haas Jul 02 '22 at 15:39
  • Thinking about this more, you have two “actions”, so presumably you have two requests, right? And you want to preserve a variable between these two requests. Is this the problem you are trying to solve? – Chris Haas Jul 02 '22 at 15:42
  • @DarkBee well you actually [can](https://phpize.online/sql/mysql57/undefined/php/php81/bddb8e70defedf58de1b0750daaacdaf/). – Your Common Sense Jul 02 '22 at 15:54
  • Basically this entire question makes no sense. Given the method names, it should be either index page or search but not both. So it's no use to pass variables in the class because it will be different HTTP requests. – Your Common Sense Jul 02 '22 at 15:57
  • alt in the path has no matter, just typing error, will correct. YES you're right - first function defines that $lang for example is DE - I try to pass the same value as variable to second function to use in actions like `$this->shipmentDict[$lang][$response['shipments'][$k]['value']];` No matter how to pass but first I try a "global variable" – Serge Jul 02 '22 at 15:58

1 Answers1

-1

I don't understand what you are trying to do. But, don't use "global" in a class. I can suggest you someting like that with what I understand, with some comments of me in it :

public function indexAction(array $myArr): array
{
    if ($this->req->get('lang')) {
        $lang = $this->req->get('lang');
     } else {
        $lang = 'en';
    }
    //svgta : I move the file put content after the if on the lang. I think you want save the data with the langs, no ?
    $myArr['lang'] = $lang;
    file_put_contents('/var/www/html/app/controllers/control_indexaction_lang.php', $lang); // output OK - en de etc...
    //file_put_contents('/var/www/html/app/controllers/control_indexaction_myarr.php', $myArr); // svgta : the output must not be OK because $myArr is an array. You should convert it, may be in json format with json_encode($myArray)
    file_put_contents('/var/www/html/app/controllers/control_indexaction_myarr.php', json_encode($myArr));
    
    //svgta : The method don't return any data. Did you not need to return $lang or $myArray ?
    
    return $myArr
}
public function searchAction(array $myArr)
{
    $lang = isset($myArr['lang']) ? $myArr['lang'] : 'en';
    file_put_contents('/var/www/html/alt/app/controllers/control_searchaction_lang.php', $lang); 
    //file_put_contents('/var/www/html/alt/app/controllers/control_searchaction_myarr.php', $myArr); // svgta : same as above
    file_put_contents('/var/www/html/alt/app/controllers/control_searchaction_myarr.php', json_encode($myArr));
    // other code of function works OK
}

As example to use :

<?php
// code before
$c = new myclass();
$myArr = [
//all you have to put in
];

$newArr = $c->indexAction($myArr);
$otherVar = $c->searchAction($newArr);

// other code
svgta
  • 343
  • 1
  • 6
  • in my case $lang is ALWAYS a single value like - en de it - etc, so actually this is not an array to be decoded. I just try to pass $lang value from first function indexAction to second function searchAction, this is why I try to use $lang as a global variable. To use under second function searchAction in lines like `$this->shipmentDict[$lang][$response['shipments'][$k]['value']];` Finally I mean, for example - if first function defines $lang as EN - then same $lang as EN should be used in second function. – Serge Jul 02 '22 at 15:12
  • Do you always call the indexAction method before the searchAction method ? – svgta Jul 02 '22 at 15:47
  • Yes, indexAction run first – Serge Jul 02 '22 at 16:00
  • I've edited my answer. Take a look on it. – svgta Jul 02 '22 at 16:15
  • Although you are right that global variables shouldn't be used, but you are missing the context of the question. You don't call an MVC controller with input variable. – Your Common Sense Jul 02 '22 at 16:18
  • @YourCommonSense I understand what you meen. But without knowing how is the controller and from where is communing the array, it's the easiest solution I can give. – svgta Jul 02 '22 at 16:26
  • Dear SVGTA, thx a lot for your help and efforts, although they've closed the question but you gave me some ideas to try. Kind regards, – Serge Jul 02 '22 at 16:53