I have this in my class
When the second function is called php errors with
wrong datatype and only variables can be past by reference.
I don't know what they mean by that
This code comes from php.net
If the same code is outside the class it executes fine
What am I doing wrong here, if I am working within a class?
$extensiesAllowed= array();
function __construct() {
$this->extensiesAllowed= array("txt", "pdf");
$this->fileName= $_FILES['file'];
}
private function isAllowedExtensie($fileName) {
return in_array(end(explode(".", $fileName)), $this->extensiesAllowed);
}
public function check_upload() {
if($this->fileName['error'] == UPLOAD_ERR_OK) {
if(isAllowedExtensie($this->fileName['name'])) {
return true;
}
}
}
the php error shows
Array
(
[bestandsNaam] => ACCOUNT INFO.txt
[extensiesAllowed] =>
)
Thanks, Richard
try putting the end and explode in seperate statements - I think end() may read by reference. In any case, it will help you figure out what line is causing you problems if it doesnt fix it.
In the second function/method you should call should be calling isAllowedExtensie as $this-> isAllowedExtensie()
if($this->isAllowedExtensie($this->fileName['name'])) {
Edit: forget my second comment..
Related
I have a PHP file that can be include'd() in various places inside another page. I want to know whether it has been included inside a function. How can I do this? Thanks.
There's a function called debug_backtrace() that will return the current call stack as an array. It feels like a somewhat ugly solution but it'll probably work for most cases:
$allowedFunctions = array('include', 'include_once', 'require', 'require_once');
foreach (debug_backtrace() as $call) {
// ignore calls to include/require
if (isset($call['function']) && !in_array($call['function'], $allowedFunctions)) {
echo 'File has not been included in the top scope.';
exit;
}
}
You can set a variable in the included file and check for that variable in your functions:
include.php:
$included = true;
anotherfile.php:
function whatever() {
global $included;
if (isset($included)) {
// It has been included.
}
}
whatever();
You can check if the file is in the array returned by get_included_files(). (Note that list elements are full pathnames.) To see if inclusion occurred during a particular function call, check get_included_files before and after the function call.
im having trouble with my php program, it seems that my array variable being declared earlier wasn't detected in a function. Here's my code :
$msg = array(
//Errors List
'Error1' => 'Error 1',
'Error2' => 'Error 2'
);
//Class for outputting Messages
class Message {
static function Info($string) { echo $string; }
static function Error($string) { echo $string; }
}
//Functions
function function1($var1) {
if (!preg_match("/^[0-9]+$/", $var1)){
Message::Error($msg['Error1']);
}
when i run it, and example i test the program like this..
$test = 'blabla';
function1($test);
it says the msg variable was undefined. Can anyone tell me how to resolve this?
Thanks in advance.
There are three ways to solve this issue.
Passing the required global var as a parameter
In my opinion, this is the preferred solution, as it avoids the pollution of your function with global variables. Global variables tend to introduce unexpected side effects and make maintenance and reuse of code a lot harder. A very extensive article on why you should avoid globals whenever possible (and some alternative solutions) can be found in the c2 wiki
function function1($var1,$mesg) {
if (!preg_match("/^[0-9]+$/", $var1)){
Message::Error($mesg['Error1']);
}
}
The call to function1 changes to
function1($test,$msg);
Using global:
Same effect as the one just below, other notation.
function function1($var1) {
global $msg;
if (!preg_match("/^[0-9]+$/", $var1)){
Message::Error($msg['Error1']);
}
}
Using the $GLOBALS superglobal
Some sources say this form is slightly faster than the one using global
function function1($var1) {
if (!preg_match("/^[0-9]+$/", $var1)){
Message::Error($GLOBALS['msg']['Error1']);
}
}
you can not use $msg as a local variable in function.
function function1($var1) {
global $msg;
if (!preg_match("/^[0-9]+$/", $var1)){
Message::Error($msg['Error1']);
}
}
I have set in myFile.php this function:
function monthLanguage()
{
if ($this->lang=='italian')//this statement is requared many times within the file!
{
$dayName[]="Dom";
$dayName[]="Lun";
$dayName[]="Mar";
$dayName[]="Mer";
$dayName[]="Gio";
$dayName[]="Ven";
$dayName[]="Sab";
}else
{
$dayName[]="Sun";
$dayName[]="Mon";
$dayName[]="Tue";
$dayName[]="Wed";
$dayName[]="Thu";
$dayName[]="Fri";
$dayName[]="Sat";
}
}
I was thinking to wrap this if statement into a function to call it where is needed as a kind of short code.
I call it like this:
monthLanguage();
but I get error message: Call to undefined function
Any help on how to reach my short code intent?
Are you including the monthLanguage function the file you are using it in? Also, I spotted two issues with this code. You are not initiating the array called $dayName and nothing is being returned so the function will not send back output. It should be like this.
function monthLanguage()
{
$dayName = array();
if ($this->lang=='italian')//this statement is requared many times within the file!
{
$dayName[]="Dom";
$dayName[]="Lun";
$dayName[]="Mar";
$dayName[]="Mer";
$dayName[]="Gio";
$dayName[]="Ven";
$dayName[]="Sab";
}else
{
$dayName[]="Sun";
$dayName[]="Mon";
$dayName[]="Tue";
$dayName[]="Wed";
$dayName[]="Thu";
$dayName[]="Fri";
$dayName[]="Sat";
}
return $dayName;
}
Also, the $this is not clear since that is usually used in the scope of a class, so perhaps you need to set the function like this:
function monthLanguage($lang)
{
$dayName = array();
if ($lang=='italian')//this statement is requared many times within the file!
{
$dayName[]="Dom";
$dayName[]="Lun";
$dayName[]="Mar";
$dayName[]="Mer";
$dayName[]="Gio";
$dayName[]="Ven";
$dayName[]="Sab";
}else
{
$dayName[]="Sun";
$dayName[]="Mon";
$dayName[]="Tue";
$dayName[]="Wed";
$dayName[]="Thu";
$dayName[]="Fri";
$dayName[]="Sat";
}
return $dayName;
}
And you would then call the function in PHP like this:
monthLanguage($this->lang);
Or like this:
monthLanguage($lang);
But it is unclear where this function is being placed or used, so clarify that to decide which is the best way to handle.
I don't think you can use $this->lang - it's usually reserved for a method if I'm not mistaken.
Try replacing the function with this, should work like a charm.
function monthLanguage($lang) {
if ($lang=='italian')//this statement is requared many times within the file!
{
$dayName[]="Dom";
$dayName[]="Lun";
$dayName[]="Mar";
$dayName[]="Mer";
$dayName[]="Gio";
$dayName[]="Ven";
$dayName[]="Sab";
}else
{
$dayName[]="Sun";
$dayName[]="Mon";
$dayName[]="Tue";
$dayName[]="Wed";
$dayName[]="Thu";
$dayName[]="Fri";
$dayName[]="Sat";
}
}
I am trying to setup an array that pulls the filename and function name to run, but it not fully working.
The code is
$actionArray = array(
'register' => array('Register.php', 'Register'),
);
if (!isset($_REQUEST['action']) || !isset($actionArray[$_REQUEST['action']])) {
echo '<br><br>index<br><br>';
echo 'test';
exit;
}
require_once($actionArray[$_REQUEST['action']][0]);
return $actionArray[$_REQUEST['action']][1];
Register.php has
function Register()
{
echo 'register';
}
echo '<br>sdfdfsd<br>';
But it does not echo register and just sdfdfsd.
If I change the first lot of code from
return $actionArray[$_REQUEST['action']][1];
to
return Register();
It works, any ideas?
Thanks
Change the last line to:
return call_user_func($actionArray[$_REQUEST['action']][1]);
This uses the call_user_func function for more readable code and better portability. The following also should work (Only tested on PHP 5.4+)
return $actionArray[$_REQUEST['action']][1]();
It's almost the same as your code, but I'm actually invoking the function instead of returning the value of the array. Without the function invocation syntax () you're just asking PHP get to get the value of the variable (in this case, an array) and return it.
You'll find something usefull here:
How to call PHP function from string stored in a Variable
Call a function name stored in a string is what you want...
I am trying to get some errors returned in JSON format. So, I made a class level var:
public $errors = Array();
So, lower down in the script, different functions might return an error, and add their error to the $errors array. But, I have to use return; in some places to stop the script after an error occurs.
So, when I do that, how can I still run my last error function that will return all the gathered errors? How can I get around the issue of having to stop the script, but still wanting to return the errors for why I needed to stop the script?!
Really bare bones skeleton:
$errors = array();
function add_error($message, $die = false) {
global $errors;
$errors[] = $message;
if ($die) {
die(implode("\n", $errors));
}
}
If you are using PHP5+ your class can have a destructor method:
public function __destruct() {
die(var_dump($this->errors));
}
You can register a shutdown function.
Add the errors to the current $_SESSION
Add the latest errors to any kind of cache, XML or some storage
If the code 'stops':
// code occurs error
die(print_r($errors));
You can use a trick involving do{}.
do {
if(something) {
// add error
}
if(something_else) {
// add error
break;
}
if(something) {
// add error
}
}while(0);
// check/print errors
Notice break, you can use it to break out of the do scope at any time, after which you have the final error returning logic.
Or you could just what's inside do{} inside a function, and use return instead of break, which would be even better. Or yes, even better, a class with a destructor.