PHP Warning: get_class() expects parameter 1 to be object, null given - php

public function InitButton($Name, $Group, $T=0, $L=0, $W=1, $H=1, $BStyle=null, $Text='', $Expire = 0, $Repeat=true)
{
$OldButton = ButtonManager::getButtonForKey($this->UCID, $Name);
//Line 4 below:
if(get_class($OldButton) == 'Button') {
$Button = $OldButton;
} else {
$Button = new Button($this->UCID, $Name, $Group);
}
$Button->T($T)->L($L)->W($W)->H($H);
$Button->BStyle($BStyle);
if(is_array($Text)) {
$Button->Text($Text[0]);
$this->bTexts[$Name] = $Text;
} else {
$Button->Text($Text);
$this->bTexts[$Name][0] = $Text;
}
$Button->Send();
$this->bState[$Name]['ID'] = 0;
$this->bState[$Name]['timestamp'] = time() - 1;
$this->bState[$Name]['override'] = false;
if($Expire > 0) {
$this->bState[$Name]['expire'] = time() + $Expire;
} else {
$this->bState[$Name]['expire'] = -1;
}
$this->bState[$Name]['repeatText'] = $Repeat;
}
PHP Warning: get_class() expects parameter 1 to be object, null given in C:\Users\HP\Desktop\test\test.php on line 4
How do i fix this?

it means that ButtonManager::getButtonForKey($this->UCID, $Name); returns null. If this is expected behavior perhaps change the if-statement to
if ($OldButton && get_class($OldButton) == 'Button')
this checks to see if the button is not null and then if the class is 'Button'
If it is not expected behavior, something is going wrong in ButtonManager

The problem might be that $OldButton can be null, and you can't call get_class on a null value. Apparently, ButtonManager::getButtonForKey($this->UCID, $Name) can return null, and that's your problem. You could fix it by doing this before line 4:
if (!is_null($OldButton)) {
if(get_class($OldButton) == 'Button') {
...
}
}

Related

php How to allow nested default value

level1();
function level1($value = null){
level2($value);
}
function level2($value = 100){}
How can I define $value of level1 as optional and still have the default of level2 applied?
Above code would not use the default because $value is already set to null by level1.
If you want the default value to be applied, you have to skip the argument completely. The easiest solution would be just to check, if $value === null
function level1($value = null){
if ($value === null) {
level2();
} else {
level2($value);
}
}
If you want to check, if the default value of level1 is applied, you could use func_num_args().
function level1($value = null){
if (func_num_args() == 0) {
level2();
} else {
level2($value);
}
}
Here's a way of doing it, it supports unlimited arguments:
function level1(){
$args = func_get_args();
call_user_func_array('level2', $args);
}
You could always do
function level2($value){
if($value === null)
$value = 100;
}
It's not the same as what you're looking for, but it has the same effect.
perhaps a bit dirty, but should work:
<?php
function level1($value = null){
($value === null)?level2():level2($value);
}
function level2($value = 100){echo $value;}
level1();
level1(7);
?>
this prints 100 followed by a 7.

difference between Null and False

i watching tutorial about developing guestbook with php
this is the code that get the message with the id
public function GetMessage($id)
{
//Database
$id = (int)$id;
$gb_host = 'localhost' ;
$gb_dbname = 'guestbook' ;
$gb_username = 'root';
$gb_password = '' ;
$connection = mysqli_connect($gb_host , $gb_username , $gb_password,$gb_dbname);
$querycheck = mysqli_query($connection,"SELECT * FROM `messages` WHERE `id` = $id");
if($querycheck)
{
$message = mysqli_fetch_assoc($querycheck);
return $message;
}
else
{
mysqli_close($connection);
return NULL;
}
mysqli_close($connection);
}
why in else statment we return NULL instead of False
what's the difference between Null and False ?
The type.
False is boolean and null is a value.
So :
$test = false;
if($test === false) {
//correct
}
$test = null;
if ($test === false) {
//incorrect
} else if ($test === null) {
//correct
}
$test = false;
if(!$test) {
//correct
}
$test = null;
if(!$test) {
//correct
}
More precision in the documentation
Imho in this case and null and false are incorrect, because method should return one type of data!
In our method it should be array not special type (null) or boolean,
and it will be easy to use this method elsewhere, because everytime we know that we works with array, and we don't have write something like this:
$messages = $dao->GetMessage(27);
if (is_array($messages)) {
// ...
}
if (is_null($messages)) {
$messages = []; // because wihout it foreach will down
}
foreach ($messages as $message) {
// ...
}
And as for me it's pretty straightforward:
if we have data at db we'll receive not empty array,
if we don't have data at db - we'll receive empty array.
It's obviously!

debug_backtrace - long parameter

I have the following function:
function backtrace($Object=false)
{
$x = 0;
foreach((array)debug_backtrace($Object) as $aVal)
{
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}
But when I use it, I'm getting an error like below:
Warning: debug_backtrace() expects parameter 1 to be long, string given in /mypath/ on line 717 ---> foreach((array)debug_backtrace($Object) as $aVal)
What's causing the error? How can I fix it?
The first parameter of debug_backtrace() is a bitmask of options (i.e. a long). It is a simple boolean true/false in PHP versions prior to 5.3.6.
To fix it, either don't pass in the $Object variable you're currently passing in or update it to be any combination of the supported options that you want to be used.
Example:
$Object = DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT;
If you want to add a pre-condition to your current block of code that will set a default value if $Object is invalid, you could try something like:
function backtrace($Object = false) {
if (!is_long($Object) || (!($Object & DEBUG_BACKTRACE_PROVIDE_OBJECT) && !($Object & DEBUG_BACKTRACE_IGNORE_ARGS))) {
$Object = 0;
}
$x = 0;
foreach((array)debug_backtrace($Object) as $aVal) {
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}
for php >= 5.3.6, you should use bitmask options
function backtrace($Object=false) {
$x = 0;
foreach((array)debug_backtrace($Object ? DEBUG_BACKTRACE_PROVIDE_OBJECT : 0) as $aVal)
{
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}

mysqli_num_rows from class function error

This is the first time I learn class in PHP, I tried to make a simple search in database.
here is some script from my class:
class DB {
...
function list_query($query) {
$ns = array();
$q = mysqli_query($this->con_(), $query);
while($n = mysqli_fetch_assoc($q)) {
$ns[] = $n;
}
return $ns;
}
...
function num_query($q) {
$num = mysqli_num_rows($q);
return $num;
}
...
}
search script :
$key = "foo";
$qsearch = $db->list_query("SELECT * FROM posts WHERE content LIKE '%".$db->escape_query($key)."%'");
$num = $db->num_query($qsearch);
if ($num == 0) {
echo "<h2>not found</h2>";
} else {
echo "<h2>result for : ".$key."</h2>";
foreach($qsearch as $val) {
echo "<h4>".$val['title']."</h2>";
echo strip_tags($val['content']);
}
}
but there is an error with the num_query() function.
with warning :
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in class/db.php on line 30.
I have checked it with manual mysqli_query() then use the num_query() function, it's work well.
sorry for my english
Your list_query() returns array. And your num_query expects mysqli_result as a parameter.
So, when you write $qsearch = $db->list_query(), you're getting an array; and then you pass that array to num_query.
Perhaps, your num_query should be like:
function num_query($result) {
return count($result);
}

I have a certain script for Feed back.

I have a certain script for Feed back. when i submit the form, it shows
"
Warning: substr() expects parameter 2 to be long, string given in /home/jetkvdmn/public_html/genFunctions.php on line 26"
the code below
<?php
$cEpro ="© Redeeming Mission 2012.";
function checkText($ElementVal) {
// If Text is too short
if (strlen($ElementVal)< 3) {
//alert('Text too small');
return false;
} else{
return true;
}
}
function checkEmail($vEmail) {
$invalidChars ="/:,;" ;
if(strlen($vEmail)<1) return false; // Invalid Characters
$atPos = stripos($vEmail,"#",1); // First Position of #
if ($atPos != false)
$periodPos = stripos($vEmail,".", $atPos); //If # is not Found Null . position
for ($i=0; $i<strlen($invalidChars); $i++) { //Check for bad characters
$badChar = substr($invalidChars,i,1); //Pick 1
if(stripos($vEmail,$badChar,0) != false) //If Found
return false;
}
if ($atPos == false) //If # is not found
return false;
if ($periodPos == "") //If . is Null
return false;
if (stripos($vEmail,"##")!=false) //If ## is found
return false;
if (stripos($vEmail,"#.") != false) //#.is found
return false;
if (stripos($vEmail,".#") != false) //.# is found
return false;
return true;
}
?>
$badChar = substr($invalidChars,i,1);
should be
$badChar = substr($invalidChars,$i,1);
^^^
You are passing i to the substr function, instead of $i

Categories