[PHP]Severity: Compile Error Message: Cannot redeclare <function> - CodeIgniter - php

I have this function:
function vai_a_capo($cont_sett){ ---> line 3
if($cont_sett >= 7){
echo "</tr><tr>";
return TRUE;
}else{
return FALSE;
}
} ---> line 10
I'm using CodeIgniter version: 3.1.2.
When I go to the link: ../index.php/home/pages/disponibilita it say to me:
Fatal error: Cannot redeclare vai_a_capo() (previously declared in C:\xampp\htdocs\CI___________\application\views\pages\disponibilita.php:3) in C:\xampp\htdocs\CI___________\application\views\pages\disponibilita.php on line 10
Thanks for the answers. Bye.
EDIT:
It has been solved. In my controller (Home) I declared by mistake twice of these:
$this->load->view('pages/disponibilita', $data);

As Paul points out.. yes you could change the function name but since your site is likely riddled with calls to this function, that would likely be a lot of work. Instead, just check to see if the function is already defined by adding 2 lines, like this...
if (!function_exists('vai_a_capo')) { // new line to go above function
function vai_a_capo($cont_sett){ ---> line 3
if($cont_sett >= 7){
echo "</tr><tr>";
return TRUE;
}else{
return FALSE;
}
}
} // new line below function
I should point out, this edit needs to go where your code is trying to define the function a 2nd time.. In \application\views\pages\disponibilita.php on line 10

You should take care of your error messages:
Fatal error: Cannot redeclare vai_a_capo() (previously declared in
C:\xampp\htdocs\CI___________\application\views\pages\disponibilita.php:3) in > > C:\xampp\htdocs\CI___________\application\views\pages\disponibilita.php on line 10
They tell you, that the method you are trying to declare has already been declared in disponibilita.php. You have to give your method another name.

Related

running using xampp error on ubuntu : Fatal error: Call to a member function rowCount() on a non-object

I'm testing a program running well using xampp and Windows 7,
But when i'm upload to the server using Ubuntu (mysql, php, apache using apt-get) it's getting error
Fatal error: Call to a member function rowCount() on a non-object in /var/www/siarsip/class/user.php on line 56
Here the code snipped :
function getAllUser(){
$query=$this->db1->query("SELECT a.user_id,a.username,a.password,a.NIP,a.role,b.category,a.input_date,a.last_update FROM users as a RIGHT JOIN user_categories as b ON a.role=b.usercat_id ORDER BY role");
$jml_data=$query->rowCount();
if($jml_data>=1){
$hasil=$query->fetchAll(); //line 56
return $hasil;
}else{
return $jml_data;
}
}
I've tried to change line 56 to :
if(!empty($query) AND $jml_data > 0) {
Still not working.
update :
Using #cjriii code, i've update line 56 to using this :
if(is_object($query))
{
{
code here
}
}
Now there's no error, i've tried to login produces same error
"Fatal error: Call to a member function rowCount() on a non-object in /var/www/siarsip/class/user.php on line 74"
function loginUser($username,$password){
$password_md5=md5($password);
$query=$this->db1->query("SELECT*FROM users WHERE username='$username' AND password='$password_md5'");
if(is_object($query))
{
$hasil=$query->rowCount(); //line 74
return $hasil;
}
}
I've insert the code again to line 74
if(is_object($query))
{
Produces no error.
But now i cannot login using the username and password that usually works.
Need another advice bro..
Two things:
Test if $query is an object before calling a method on it. Thats' where the error comes from.
Second, instead of testing for !empty($query), try testing for is_object($query).
if(is_object($query))
{
$jml_data = $query->rowCount();
if($jml_data >= 1)
{
//rest of your code here
}
else
return $jml_data;
}
Edit:
Please check the documention on empty(): Your call may be returning a valid value for empty to return false.
http://php.net/empty
http://php.net/manual/en/function.is-object.php

Error - Trying to get property of non-object

I am currently getting this error on one of my files:
/var/www/vhosts/httpdocs/generator/index.php on line 6
Line 6 would be resulting in:
echo $results->GetBookCodesResult->BookCodes->BookInfo->ServiceCode;
My code:
<?php
$config['soap_url'] = "http://myservice.com?WSDL";
if(isset($_REQUEST['codes'])) {
$results = request_Data(explode("\n",$_REQUEST['codes']));
echo $results->GetBookCodesResult->BookCodes->BookInfo->ServiceCode;
}
....
What is the best method to have this fixed? Some advice would be appreciated.
your "->BookCodes" sub-property is empty as you can see in your var_dump-output...
this property "BookInfo" just not exist.. maybe no books are found? ;)

Throwing errors from its "correct" source

I hope the title isn't too confusing, I'll try to explain better below.
Suppose I have a function in a separate file, functions.php:
function divide($num1, $num2) {
if ($num1 == 0 || $num2 == 0) {
trigger_error("Cannot divide by 0", E_USER_ERROR);
} else {
return ($num1 / $num2);
}
}
And another file that calls it:
include "functions.php";
echo divide(10, 0);
My error is
Fatal error: Cannot divide by 0 in
C:\Users\Derek\Desktop\projects\functions.php on line 5
My question is, how do I make that error instead point to the location of the error in the main code, so I instead get:
Fatal error: Cannot divide by 0 in
C:\Users\Derek\Desktop\projects\main.php on line 3
The particular reason I want this is because I have a function called load_class that simply finds a PHP file and instantiates the object inside, but if given an incorrect file name, it reports an error from inside load_class, which is technically true, but it's not particularly helpful if I don't remember where I called load_class in the first place. I would like the error to point to the file that called load_class incorrectly.
Also, I would like to write a function error() (something like below) that when given a message as a parameter would throw more "meaningful" error messages, but when done that way, the error always says it comes from error(), not from where the error actually came from!
For example, in an error.php:
/**
* error()
*
* Throws an error of a certain type
*
* #param string $type The type of error. "Fatal", "warning," or "notice"
* #param string $message A description of the error
* #return void
*/
function error($type, $message) {
switch (strtolower($type)) {
case 'fatal':
trigger_error($message, E_USER_ERROR);
break;
case 'notice':
trigger_error($message, E_USER_NOTICE);
default:
trigger_error($message, E_USER_WARNING);
break;
}
}
And in an index.php
error("fatal", "A sample warning!");
My error given is:
Fatal error: A sample warning! in
C:\Users\Derek\Desktop\projects\synthesis\sys\Error.php on line 45
But the error didn't occur in error.php, it happened in index.php! How can I make it show where it really came from?
The debug_backtrace function allows you to obtain the stacktrace as an array. You can pick the original location from there.
Next to that you need to slip into the error message to make this look-alike. Example:
function divide($num1, $num2) {
if ($num1 == 0 || $num2 == 0) {
trigger_error_original("Cannot divide by 0", E_USER_ERROR);
} else {
return ($num1 / $num2);
}
}
function trigger_error_original($message, $type) {
$trace = debug_backtrace(FALSE);
list($location) = array_slice($trace, 1, 1) + array('file' => 'unknown', 'line' => 'unknown');
$message .= sprintf(" in %s on line %d\nTriggered", $location['file'], $location['line']);
trigger_error($message, $type);
}
divide(1, 0);
The error message than shows something like:
> php test-pad.php
Fatal error: Cannot divide by 0 in test-pad.php on line 18
Triggered in test-pad.php on line 15
The downside of this is, that you need to change your code to have this "feature". If you need this for debugging your own code, it's much better that you enable backtraces in your logs. The Xdebug extension does this for you, or you can write your own error handler that takes care of that.
See as well the related question Caller function in PHP 5?. I used array_slice so that you could create an additional parameter to define the number of steps you want to go "up" in the backtrace.
Use debug_backtrace(), and debug_print_backtrace() for a full call stack. These are especially effective when using Xdebug, which will override the function to colorize the output.
I have this same problem...
#1: while 10/0 = ERROR, 0/10 = 0 is perfectly legal, you shouldn't have an exception for that.
#2: when you include a file, it effectively becomes part of this new file, so perhaps you might have to toy a little bit with things like __FILE__ and see if you can make it point it to the file before it gets included in the other file..
You can use xdebug - it will show you the stacktrace or you can register your own error handndler and display the stacktrace. Just check the example in php.net for set_error_handler().
Maybe exceptions are better to use in your case. You get the full stacktrace and can locate where the function was called without relying on some tricky code :)

PHP Class Variable Access

Seems like a simple enough question, so my apologies for asking. As a precursor I'm not necessarily 'new', but rather not so well-versed in PHP.
I have a class, declared as follows:
class User
{
public $id = "";
public function User()
{
$this->$id = isset($_COOKIE['userid']) ? $_COOKIE['userid'] : 0;
}
}
Which seems simple enough, however - upon construction, I get the following set of errors:
Notice: Undefined variable: id in D:\xampp\htdocs\sitecore\include\classes.php on line 13
Fatal error: Cannot access empty property in D:\xampp\htdocs\sitecore\include\classes.php on line 13
Sorry for asking something so simple. The line in question starts with "$this->$id".
Remove the $ symbol at the 'id' place:
$this->id = isset($_COOKIE['userid']) ? $_COOKIE['userid'] : 0;

Get filename and line number of start of function declaration [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find out where a function is defined?
included php file to know it's file name?
Can I get the filename and line number of the start of a function declaration in PHP?
Say, I have the following code:
1. /**
2. * This function shows the foo bar.
3. */
4. function foo_bar($subject) {
5. echo "Foo bar:\n";
6. if ($subject == "none") {
7. trigger_error("Wrong argument given to 'foo_bar()' in ... on line ....", E_USER_WARNING);
8. }
9. ...
10. }
When I call foo_bar("none"), the function must throw an error like this:
Warning: Wrong argument given to 'foo_bar()' in /home/web/mcemperor on line 4.
Can I retrieve the filename and the line number of start of the function declaration?
You're looking for ReflectionFunction.
$r = new ReflectionFunction('foo_bar');
$file = $r->getFileName();
$startLine = $r->getStartLine();
It's that simple... It works for any defined function, whatever one that you passed in to the constructor argument.
The function name could be found with PHP exceptions, which are more interesting for error triggering : http://www.php.net/manual/en/exception.gettrace.php
$backtrace = debug_backtrace();
$line = $backtrace[0]['line'];
$file = $backtrace[0]['file'];
http://www.php.net/manual/en/function.debug-backtrace.php

Categories