I was unable to find a similar question on Stackoverflow, although I am sure someone has probably asked this before.
I have a class with methods that may be called several times per page. Each time the method is called I need to make sure the public variables are reset to their defaults, UNLESS they have been set before calling the method.
This cannot be achieved using a simple if condition because there is no way to tell whether the value has been set or is still set from the last method call
I cannot think of a way to achieve this because I cannot call my __construct method (which sets all the default values), as this would overwrite the parsed values. However, I need to reset them to prevent values from the last method call from being parsed.
The obvious answer is to give different names to the public variables and the return variables. I will do this if there is no other option but I like to keep the number of variables to a minimum
It is very hard to explain this in writing so I will update this question with an example of what I mean in code.
UPDATE
An example of where a problem may occur:
<?php
class test{
public $return_array;
public $return_string;
public $return_bool;
function __construct(){
// Set the default values
$this->return_array = false;
$this->return_string = false;
$this->return_bool = false;
}
public function method(){
// ... do something
$array = array('test');
$string = 'test';
$bool = true;
// Only return variables if asked to
$this->return_array = $this->return_array ? $array : NULL;
$this->return_string = $this->return_string ? $string : NULL;
$this->return_bool = $this->return_bool ? $bool : NULL;
return;
}
}
// Initiate the class
$test = new test;
// Call the method the first time with one parameter set
$test->return_array = true;
$test->method();
// Print the result
print_r($test->return_array);
// MOST OBVIOUS ANSWER WOULD BE TO RESET VARIABLES HERE LIKE SO
$test->reset(); // HOWEVER, I DO NOT WANT TO HAVE TO CALL THIS EACH TIME I CALL THE METHOD, HERE LIES MY PROBLEM!
// Call the method again with different parameters
$test->return_string = true;
$test->return_bool = true;
$test->method();
// Print the result
echo $test->return_array;
echo $test->return_bool;
/* The problem lies in the second call of the method because $test->return_array has not been reset to its default value. However, there is no way to reset it without affecting the other variables. */
?>
This is basically a very long winded way of asking whether it is possible to reset a classes variables to their default values, whilst ignoring the ones that have been parsed to the method being called
There are several ways to achieve this but they all bottle down to the same solution. Calling a function after each method that resets the variables within the class. Best way to do this is at the end of each method before the data is returned.
Related
I have this variable on class/Controller level $songRating and i am calling this method through ajax, when i first time call this method it runs the if block which is good. and now $songRating should be 1.
But this is not a case. When i call this method it again runs the if block. Dont know why :/
public $songRating;
public function GetHighRatedSong()
{
if($this->songRating == null){
$this->songRating=1;
}else{
$this->songRating=2;
}
return response()->json($this->songRating);
}
Try with Replacing
$this->songrating=1;
to
$this->songRating=1; # song+Rating != song+rating
Read PHP & Case Sensitivity
It's because everytime you call that function or make a new instantiation to the class, it will automatically reset to it's original value. Try to use service container
I'm creating a website and somewhere in the code I need to query for a user attribute (ex:account state) and in the same row I have the reason, case is account state is "suspended".
I'm trying to minimize the requests to the database, so I created a function to verify account state.
function getAccountState($userid,$reason){}
What I am trying to do is if account state is "suspended" I would change the $reason to "the database reason".
I've already done that but if I change the $reason inside the function, outside the function it will not change.
I searched for "php pointers" on google but I think there is not such thing.
Is there a way to do this? Other way I'll just make another database request...
You could of course pass the variable by reference but as you don't seem to need it, I would just return it from the function:
function getAccountState($userid){
// your code
return $reason;
}
and call it like:
$reason = getAccountState($userid);
If you want to stay your code as it is now, you could pass the variable by reference:
function getAccountState($userid,&$reason){}
^ like so
You could consider passing it in by reference. Or perhaps just changing the function to return the correct information.
In the definition of functions you can tell that $reason argument is passed by reference, not value. To do so, use & in front of variable:
function getAccountState($userid,& $reason){}
You are looking for references, in PHP terminology, not pointers.
They work this way :
function getAccountState($userid, &$reason){ // Notice the &
$reason = "database locked"; // Use it as a regular variable
}
getAccountState(12345, $reason); // Here, it is written as a regular variable, but it is a ref.
echo $reason; // echoes "database locked"
I have a page named ChangeApprovalInfo.php - It has a function called Row_Rendered as follows;
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$RecordOwner = $this->RequestUser->CurrentValue;
echo $RecordOwner;
}
Echoing $RecordOwner gets me the data I will need for a sql query on another page....
I have another page called ChangeApprovalEdit.php - This page has
<?php include_once "ChangeApprovalinfo.php" ?>
at the top of the file.
ChangeApprovalEdit.php has a function where I need the $RecordOwner variable as defined in ChangedApprovalInfo.php
If I add "echo $RecordOwner" on the ChangeApprovalEdit.php page, I get an error saying it's an unknown variable. My understanding is that I need to "make it global" or some such business. I know very little about PHP and the pages I am editing are long and complex. (to me, at least)
What do I need to do? I know that the information I have provided might not be enough to answer the question. I don't know enough to even know exactly what I need to ask. If more information is needed, I will edit and follow up.
pastebin of the files
ChangeApprovalInfo.php = http://pastebin.com/bSRM1wwN
ChangeApprovalEdit.php = http://pastebin.com/AStG9pqb
EDIT:
Changing Row_Rendered to this seems to be more effective. I'm having trouble seeing WHERE I can later echo this variable... but I'm getting somewhere with this...
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;
}
Don't echo variables from functions, which just outputs them to the standard output. return them from the function so you can use the value elsewhere as well.
function Row_Rendered() {
$RecordOwner = $this->RequestUser->CurrentValue;
return $RecordOwner;
}
Then instead of
$obj->Row_Rendered();
use
echo $obj->Row_Rendered();
and if you want to use the value elsewhere, use
$value = $obj->Row_Rendered();
You can do a couple of things:
First, you can return $RecordOwner from the function, and store its value in a variable. This method is usually preferred.
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$RecordOwner = $this->RequestUser->CurrentValue;
echo $RecordOwner;
return $RecordOwner;
}
// Store it in a variable when calling the function.
$RecordOwner = Row_Rendered();
Or, you can make it global inside the function:
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;
echo $GLOBALS['RecordOwner'];
}
You can use the $GLOBALS superglobals array, like this:
function Row_Rendered() {
$GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;
}
However, you should not do that. Instead, refactor your application so that the view in ChangeApprovalinfo.php just contains a function, which is then called with the appropriate parameters.
EDIT: Chaning Row_Rendered to this seems to be more effective. I'm having trouble seeing WHERE I can later echo this variable... but I'm getting somewhere with this...
function Row_Rendered() {
// To view properties of field class, use:
//var_dump($this-><FieldName>);
$GLOBALS['RecordOwner'] = $this->RequestUser->CurrentValue;
}
I feel compelled to write another answer to this update. Let me demonstrate the use of globals as seen from outside that function:
$obj->Row_Rendered();
$obj->foobar();
echo $GLOBALS['RecordOwner'];
Quick, what will be echoed and where does that value come from? Well, it depends on what $obj-foobar() does. Maybe it changes the global variable. Maybe it doesn't. Who knows if the variable has been set at all? How would you trace back what happened exactly without adding a debug line after every single function call?
And that's just three lines of code. Imagine that in an application of any complexity.
Now, the same thing if I return the value from Row_Rendered:
$owner = $obj->Row_Rendered();
$obj->foobar();
echo $owner;
If the Row_Rendered method is behaving as it should (returning the owner), this code is very predictable. If you do not follow this pattern, you'll have a hell of a time getting anything done when the application grows to any halfway complex size.
Set the variable as global from within the function
$my_global_var = "old value";
function doing_stuff(){
global $my_global_var; //this will use the global variable instead of creating a local one
$my_global_var = "new value";
}
echo $my_global_var;//returns "new value"
I want to pass through configuration arguments to a class. These are all the optional vars that go into configuring the class - and should be able to run in any order.
at the moment i just pass through the optional vars the regular way. Supposing the constuctor was like the following:
private var _reqVar:String;
private var _optVar1:String;
private var _optVar2:String;
public function Constructor(reqVar:String, optVar1:String = "empty", optVar2:String = "empty){
// set the variable to equal the arguments here...
}
the problem with this is for the end user, where instantiating the class isnt particularly readable (especially when the argument list can grow quite large)
ideally i would like to pass the arguments though similar to this:
var instance:ClassType = new ClassType(reqVar, {width:100, height:100, speed:4, lives:3})
which again is fairly straight forward. where i stumble over are the following points:
assigning the argument to the var of the same key (i know in php to reference a variable name from a key you can use $$key = $value, is there an equivalent in as3?)
display an error (using the 'throw' method) for variable names not supported by the class
any help appreciated.
Updated answer:
public function Test(reqVar, optionalVars:Object)
{
for (var prop:String in optionalVars)
{
try
{
this[prop] = optionalVars[prop];
}
catch (error:Error)
{
throw new Error("Unknown property: " + prop);
}
}
}
So the answers to your questions are:
You assign to a property or variable by name using object[NAME], in this case this['propertyName'].
You can check whether a property exists using hasOwnProperty. Update: If you also want to assign to variable other than properties, I don't think you can check if they exist. To solve this, you could either just assume they exist, assign to them and get an error when you use an invalid variable name. You could also wrap the assignment in a try ... catch block and throw a prettier exception as I've shown above.
In PHP I know many people will use a class to SET and GET session variables, I am doing this now in many classes, I need to know if I am doing it wrong though.
So for example lets pretend I have A class that need to use this
$session->get('user_id')
Which gets this value
$_SESSION['user_id']
Now in this class if I have 15 methods and in each method I need to access this value several time, currently I am calling $session->get('user_id') 20 times in a class if it is needed 20 times, should I be setting this 1 time per class to a local variable for that class and then access it? I am not sure if it makes any difference or not, my theory is that the way I am doing it now is 20 extra function calls that could be avoided?
If my theory is correct, what would be the best way to store these values inside a class? Like a private or public or protected variable?
Thanks, sorry for any confusio, classes and objects are taking me a while to learn.
Also note that $session->get('user_id') is just 1 of many DIFFERENT variables I would need to do the same thing to as well.
UPDATE
After reading Chacha102's post about using an array() ... here is what I have tried, does this look like a good way or still can be improved a lot?
class file
<?PHP
class User
{
// Load user details into an Array
public function load_user()
{
$this->user_id = $this->session->get('user_id');
//if user ID is already set, then Load the cached urser data
if(isset($this->user_id) && $this->user_id != ''){
// set user data to an array
$this->user['user_id'] = $this->user_id;
$this->user['user_name'] = $this->session->get('user_name');
$this->user['pic_small'] = $this->session->get('pic_small');
$this->user['sex'] = $this->session->get('sex');
$this->user['user_role'] = $this->session->get('user_role');
$this->user['location_lat'] = $this->session->get('location_lat');
$this->user['location_long'] = $this->session->get('location_long');
$this->user['new_user'] = $this->session->get('new_user');
return $this->user;
}
}
}
?>
main page file
<?PHP
require 'user.class.php';
$user = new User;
// if a user_id is set into a session variable then we return an array of other user related data
$user->account = $user->load_user();
// would show the user's ID from our array
echo $user->account['user_id'];
?>
If you are doing something like this:
if($session->get('user_id')==1)
{
$prefs = get_prefs($session->get('user_id'));
$info = get_info($session->get('user_id'));
}
then I would replace it with a since local variable
$id = $session->get('user_id');
if($id == 1)
{
//.....
}
It increases clarity for one. It probably isn't a big deal to call a simple function like that over and over again, but I still wouldn't do it.
I try to reduce the number of functions I call in a single method. If you are doing something like:
$user_id = $session->get('user_id');
$name = $session->get('name');
// ... etc ...
You might just want to grab an array of all the session variables instead.
$user = $session->get_array();
echo $user['user_id'];
This reduces the function calls, and you get all the data in one fell swoop.
Just one thing on clarity, using an array of user data is probably easier to read than to create a variable for each thing ($user_name, $user_id, etc).
For accesses distributed over a number of methods, as long as you're just using the function to access the variable, I'd say stay with the function. The additional cost is minuscule, and it's better for long term maintainability.
Within the same method, you would make one function call, populating a local variable, as Chacha102 suggests.
Even if the function does resource-intensive things like database calls, I would prefer giving the function some internal caching before adding a member to your class.
Adding the variable as a member to your class doesn't really make sense in the OOP way, because it's not a logical, legitimate member of the class but just a temporary variable.