I have a following php function:
public function getTrailer()
{
return $this->model->trailer;
}
How to assigned value of $this->model->trailer to $new variable. And can you explain me what $this->model->trailer means, please ?
Thanks !
To assigned the value to a new variable do this:$newVar = $this->model->trailer;
The meaning of $this->model->trailer is that is taking the value that are inside of the structure. Is looking what are inside $this, and inside the model and inside of trailer and taking the value.
Related
Say I have a method (which in this particular case is a static method), and this method works on a given value. Once completed is there a way that I can automatically in the code, delete the variable (rather than the function copy).
I suspect from all I've read that this is not possible, but there are no clear declarations of such that my searching has found.
An example case:
Static Method:
public static function checkKey($keyValue = null)
{
if(!is_null($keyValue) && !empty($keyValue)) {
if($_SESSION['keyValue'] == $keyValue) {
unset($keyValue,$_SESSION['keyValue']);
return true;
}
unset($keyValue,$_SESSION['keyValue']);
return false;
}
return false;
}
Usage:
$valueToBeChecked = "I want this value unset from within the function"
//PHP page code
AbstractClass::checkKey($valueToBeChecked);
Is there a way that the method checkKey above can delete the value of $valueToBeChecked from within the method checkKey?
The fact it's a static method shouldn't be too critical, it's more the shape of is there a way that the function can delete a value that is set outside the funtion/method, when passed the variable as a parameter?
I realise this is possible if the whole thing is wrapped in a Class and the variable is saved as a class level variable (unset($this->var)), but I'm curious if there's any ability to "reach" variables from outside the scope such as
public static function checkKey($keyValue = null)
{
unset(\$keyValue);
}
I only have limited experience with namespacing but that's my best guess as to if this is possible, how to go about it.
simplified equiviliant outcome:
What I'm trying to reach is this action, entirely within the method:
$valueToBeChecked = "something"
AbstractClass::checkKey($valueToBeChecked);
unset($valueToBeChecked);
You cannot unset a variable from within a function and have that effect propagate. Per the manual:
If a variable that is PASSED BY REFERENCE is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
However, you can get equivalent behavior through pass-by-reference and setting to null:
function kill(&$value) {
$value = null;
}
var_dump($x); // NULL
$x = 'foo';
var_dump($x); // 'foo'
kill($x);
var_dump($x); // NULL
This works because, in PHP, there's no distinction made between a symbol that doesn't exist and a symbol that exists with a NULL value.
$users = [
"Andrew",
"Max",
"Larry",
"Ricardo",
"Lucy",
"Marcus",
"Sophie"
];
$sector_rel = [];
$location_rel = [];
function sectorRel($user){
return sector_rel[] = round(1/rand(1,10),3);
}
function locationRel($user){
return $location_rel[] = round(1/rand(1,20),3);
}
foreach($users as $user){
sectorRel($user);
locationRel($user);
}
This:
function sectorRel($user){
return sector_rel[] = round(1/rand(1,10),3);
}
Should be/could be:
function sectorRel($user){
global sector_rel;
sector_rel[] = round(1/rand(1,10),3);
}
The problem is that the functions don't have access to the array variables. You can import them into the function scope using the keyword global, if they are indeed global variables. Now, having global variables isn't a good thing, and for a small test it's okay, but eventually you'll be eliminating your globals and this solution won't work.
But alternatively, you could pass the array variables to the function as an argument. However, this still introduces a lot of logic in the function. The function has to be told about the array, it must know that it needs to add a value to the end, and it also needs to calculate the actual value to add.
So better, make the function just return the calculated value and add it to the array outside of the function:
function sectorRel($user){
// Assuming your are going to use 'user' here somewhere?
return round(1/rand(1,10),3);
}
function locationRel($user){
return round(1/rand(1,20),3);
}
foreach($users as $user){
sector_rel[] = sectorRel($user);
$location_rel[] = locationRel($user);
}
You can then wrap this entire snippet of code into another function and call that to populate the arrays. That way, you've quite reasonably split the responsibilities of the functions and have a piece of code that looks nice and clean.
You do not need to use return in either of sectorRel or locationRel. At the moment this will return the reference to that array and it is not being stored in a variable. You would need to store them in a variable or just get rid of the return. My PHP is a little weak at the moment but you should probably append the values in those functions to the array.
Also if you have a parameter called $user for each of those functions you should either use that parameter or just get rid of it.
I have a PHP function that I need to create that executes PHP or HTML Code that is stored in a variable. That's one problem. Another problem is the fact that the function will be created multiple times and will need a unique name so I've named it after $title but I'm not sure if this will create the function from the value of $title or if it will create a function called $title or if it just won't work. This is my code:
$title = $_POST['sogo_aso_name'];
$output = $_POST['sogo_aso_output'];
Some Code Here...
function $title ($output)
{
//Some Code Here to execute $output...
}
So... That's my problem. I'm not sure what will execute the $output cleanly so it can be shown on a page. And I don't know if the function will be named after the value of $title
Thanks for any help!
Ethan Brouwer
You can use call_user_func like this:
call_user_func($title, $output);
But it's really strange to change name of one function.
What you're trying to do is unfortunately not possible. You could create namespaces if you need a common name to prefix your functions with.
namespace fArr {
function func1 (args) {}
function func2 (args) {}
}
To call them you can use:
namespace {
fArr\func1($title, $output);
}
Generally you want to avoid creating totally anonymous functions. Maybe create a function that handles the title and output for all requests. Alternatively create a new object with the func name as a property and output assigned to it.
$title = $_POST['sogo_aso_name'];
$output = $_POST['sogo_aso_output'];
// Store data in an object named after the value of $title
// Note that constant use of this can get very messy, and very confusing
$$title = (object) array('output' => $output);
suppose I need to use a variable's name as the function name of call_user_func_array()Docs
eg.
$j = new SomeObject();
and I'm trying to call $j->funcme();
call_user_func_array('$j->funcme()',$args);
returns the not found or invalid function name error
what should I do to rectify this?
Use it with an array for the Callback:
call_user_func_array(array($j, 'funcme'), $args);
See: call_user_func_arrayDocs
I dont know what we call this, but it will be something like this
$args = 1
function rendercomments($args,$usersholder = NULL ){
$args = $args + 1;
// missing
return true;
}
changeargs($args);
$userholder = 2
Can we do something like that?
Someway to make $args append to $usersholder without returning it?
Yes, all you need is to define your arguments "by reference" by using the ampersand (&).
function rendercomments(&$args, $usersholder = NULL){
$args += 1;
return true;
}
$args = 1;
rendercomments($args);
echo $args; // 2
I believe what you are asking for is to update the $userholder variable while inside the called function.
If so, you have two options:
Create a class that contains the $userholder and the changeargs($args); should be inside that class and you can just use the $this keyword to apply any changes to the $userholder object and the changes will continue outside of the changeargs() function.
Pass into the changeargs() function a reference to the $userholder variable so that you are modifying that variable the whole time.