I'm writing a form-helper class in php, that should check if a value is submitted via the method, the form defined.
public function getValue($name) {
$getFrom = ($this->method == 'post') ? '_POST' : '_GET';
return isset($$getFrom[$name]) ? $$getFrom[$name] : null;
}
$this->method is post or get, it's form-dependent.
This method looks fine for me, but php throws a "Illegal string offset" warning. What can I do without using an if-block.
Thank you!
I know that the replies have been an attempt to help the user with a better approach, but they do not answer the question. Why is it throwing the error that he sees? I just tested this to ensure I was correct in my assumption.
When he uses $$getFrom[$name], PHP first parses "$getFrom[$name]". That is what is throwing the error. He obviously wants $getFrom to be parsed without including [$name]. So, he needs to use ${$getFrom}[$name].
Try this,
public function getValue($name) {
return isset($_REQUEST[$name]) ? $_REQUEST[$name] : null;
}
Related
I have this php function to add an object to the database, but it is giving me some troubles to make it work. I'm pretty used to the java way, so i tried to follow the same pattern, and I'm getting an error message that says "Strict standards: Only variables should be passed by reference".
This is the code:
public function saveUser(User $user){
$this->connection = DaoFactory::openConnection();
$this->query = $this->connection->prepare($this->SAVE_QUERY);
$this->query->bindParam(':name', $user->getName());
$this->query->bindParam(':lastName', $user->getLastName());
$this->query->bindParam(':age', $user->getAge());
$this->query->bindParam('gender', $user->getGender());
$this->query->bindParam(':email', $user->getEmail());
$this->query->bindParam(':password', $user->getPassword());
$this->query->execute();
$this->connection = null;
}
I've searched and i found that the object attribute should be placed in a variable to avoid this issue, but by doing so, the code gets very messy and complex.Example:
$name = $user->getName();
$this->query->bindParam(':name',$name);
Is there any other way to solve this?
bindParam expects a variable passed by reference, you're instead giving it a value returned from a function, which cannot be passed by reference. So instead, use the other API method bindValue() to bind your values.
See https://stackoverflow.com/a/14413428/476 for what the difference is.
I am trying to make a small survey form asking people questions and replying either yes or no.
I am trying though to execute the the first two functions on the 3rd function and also check if the answers were set but i get a:
Fatal error: Can't use method return value in write context in.
Can someone please help me or point me to the right path?
<?php
class survey {
... some functions ...
function check($rep1, $rep2){
if (isset($this->q1($rep1)) && isset($this->q2($rep2))) {
####################### #######################
echo "Thank you for the feedback";
}elseif (! array_key_exists(#$_POST['answer'], $var)) {
echo "Please select an option<br/>";
}
}
}
?>
The error is inside the check function on the first line.
Since functions always return something (even if it's NULL), using isset() on them is nonsensical.
isset is a language construct that takes a variable name and tells you if it exists.
In this case, why not just write if(isset($_POST['answer']))?
I am creating a function that converts a users initials (STRING) to their userid (INT)
problem is when I call the function I get a call to undefined func error because the below declared function is no where to be found in the Source!
// connect to database -- this works, I checked it
function convertRadInitToRadID($radInits){
$sqlGetRadID="SELECT id FROM sched_roster WHERE radInitials == '".$radInits."'";
$resultGetRadID=mysql_query($sqlGetRadID);
$radID=mysql_result($resultGetRadID,0);
return $radID;
}
...I then create and array ($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter) of user initials, it works with no errors I tested it independently
$randKey=rand(0,(count($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter)-1));
$randRad=$radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter[$randKey];
$randAssignedRadID=convertRadInitToRadID($randRad); // call to undefined function error here
when I view source the function definition code (where I define the function) is nowhere to be seen in the source. Very strange. I tried placing it around different areas of the script, same error. The function definition is declared appropriately and is wrapped in .
Very strange. The function just doesn't appear. Quotations, syntax, semi-colons, etc are all spot on.
No syntax errors. Advice?
I Strongly agree with Answer #1.
In addition a usual problems occur in php if you are defining function after calling it. i.e. your calling code is before function defination then it will not run and will give an error of undefined function.
You can create a class then define this function in that class and on the time of calling you can call that function with help of $this->function(args)
I think this will resolve your problem in mean while i am trying to run your code on my machine, lets see what happen
May be your function is a method of some class. So, if it is, you should use it in another way:
MyClass::convertRadInitToRadID($radInits) // if static method
or like this
$class = new MyClass();
$class ->convertRadInitToRadID($radInits)
Trying to make sense of your question... Are you trying to call the function using JavaScript? If so, remember that JavaScript is run on the browser, and PHP is run on the server (and this is why when you "view source" you don't see the function anywhere). To send data back from JavaScript to PHP you should use AJAX.
I found the answer: I was using Jquery UI tabs.... there must be a conflict with tabs. When I run the code without tabs there is no issue.
Thanks for the '==' fix.. appreciate it. my bad
thanks for reminding me about the 80 char varname code limit
I'm having an issue with the following code. I'm just guessing it's a scope problem.
public function run() {
return eval('$this->config();');
// This will return null.
return $this->config();
// This will return my config array right.
}
Before anyone asks:
Not both return are 'active' when testing.
I know eval is evil, but i'm building some kind of terminal for admins to run PHP code.
Do anyone have any suggestions about this terminal thing? (My basic problem is still with the eval...)
eval is returning NULL because that is what it is suppose to do. Right from the documentation page:
eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned.
I'm having a problem creating webservices through nuSOAP (although i believe my problem has nothing to do with it)
What i'm trying to do:
function loadActiveItems() {
$list = Item::loadActive();
$ret = array();
foreach ($list as $val){
//two tests to check if i really have an object and if the toDTO method is callable
echo var_dump($val);
echo is_callable(array($val, 'toDTO'));
array_push($ret, $val->toDTO());
}
unset($val);
return $ret;
}
I'm getting the following error:
Call to a member function toDTO() on a non-object
and both var_dump($val) and is_callable are returning the expected (the object and true, respectively) from what i've been seeing online, it appears i have a out of scope problem... but for some reason i don't seem to get my head around it :P
Thanks in advance
EDIT: well just check that apparently i don't understand is_callable either because i always get 1 as the result...
EDIT2: i'm using php-activerecord if that helps in any way
toDTO() may be undefined in your class Item.
Another reason may be that the method isn't public or as #Grep said` static.
This error never happens on an object that defines the method but it is static or protected/private:
Call to a member function toDTO() on a non-object
That error only happens if $val is not an object. Usually a NULL, FALSE or other scalar.
It's usually a FALSE when the object came for a db_fetch() function but the fetch or the query before it failed.
It's usually a NULL when you have an array that may have NULLs in it.
var_dump($list) and see what's in there and if there are any NULLs. Also change your foreach to have a $key and var_dump($key) as well to see which key is dumped last before the error is issued.
Okay so i figured out the problem... thanks for all the help!
I was calling toDTO of another object inside toDTO... problem was that object could be a null!
So a simple if(object==null) solved the problem!
Thanks again!