This question already has answers here:
Is there a way to check if a function exists within a class?
(3 answers)
Check if method exists in the same class
(4 answers)
Closed 3 years ago.
I want to use some code only if the method getProductgroup exists.
My first approach:
if(isset($item->getProductgroup())){
$productgroupValidation = 0;
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup){
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
I got the error message:
Compile Error: Cannot use isset() on the result of an expression (you
can use "null !== expression" instead)
if(($item->getProductgroup())!==NULL){
$productgroupValidation = 0;
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup){
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
But like this I also get an error message:
Attempted to call an undefined method named "getProductgroup" of class
"App\Entity\Documents".
You can use the function method_exists to check if the method is existing in a class or not. for example
if(method_exists('CLASS_NAME', 'METHOD_NAME') )
echo "it does exist!";
else
echo "nope, it is not there...";
In your code try
if(method_exists($item, 'getProductgroup')){
$productgroupValidation = 0;
if(method_exists($item->getProductgroup(), 'getUuid'))
{
$productgroupId = $item->getProductgroup()->getUuid();
foreach($dataField->getProductgroup() as $productgroup)
{
$fieldProductgroup = $productgroup->getUuid();
if($productgroupId==$fieldProductgroup){
$productgroupValidation = 1;
}
}
}
}
Related
This question already has answers here:
php filter_var fails with zero on FILTER_VALIDATE_INT
(2 answers)
Closed 5 years ago.
I had issue about FILTER_VALIDATE_INT function when i put 0 as entry value it show 'error' instead of 'ok':
$delivery = 0;
if (filter_var($delivery,FILTER_VALIDATE_INT)) {
echo 'ok';
}else{
echo 'error';
}
Compare with false, because PHP consider zero returning as false
if (false !== filter_var($delivery,FILTER_VALIDATE_INT)) {
Check for strict type check it will work,
$delivery = 0;
if (filter_var($delivery,FILTER_VALIDATE_INT) !== false) {
echo 'ok';
}else{
echo 'error';
}
Demo
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I got error in my data viewing page, what is the correction for my php script
Here is my php script:
Notice: Undefined index: o in /home/tz005/public_html/COMP1687/view_data.php on line 64
<?php
$con=mysqli_connect("mysql.cms.gre.ac.uk","tz005","punceg5L","mdb_tz005");
if (is_numeric($_GET['o']))
{
$o=$_GET['o'];
}else {
$o=0;
}
if ($o >=1){
$prev=$o-1;
} else{
$prev=0;
}
You need to add an isset() function to check if $_GET['o'] even exists.
Read more about isset() Here
use this:
<?php
$con=mysqli_connect("mysql.cms.gre.ac.uk","tz005","punceg5L","mdb_tz005");
if (isset($_GET['o'] && is_numeric($_GET['o']))
{
$o=$_GET['o'];
}else {
$o=0;
}
if ($o >=1){
$prev=$o-1;
} else{
$prev=0;
}
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
I am working on a php method that giving me a syntax error on the third line of the method body. Commenting out the line does not do anything for me. Only by commenting out the entire method, does it actually go away. Am I missing something here?
public function get(Array $array) {
if(array_key_exists('data', $array)) {
if($array['data'] == '*') {
$fieldString = '*';
} else {
$fieldString = '';
for($x=0; $x < count($array['data']); $x++) {
if($x == count($array['data']) - 1 ) {
$fieldString .= $array['data'][$x].' ';
} else {
$fieldString .= $array['data'][$x].', ';
}
}
}
if(array_key_exists('table', $array)) {
$table = $array['table'];
}
if(array_key_exists('conditions', $array)) {
$condition = $array['conditions'];
$filter = '';
foreach($condition['cond'] as $cond) {
if(array_key_exists('type', $cond)) {
$filter .= $cond['type'].' ';
}
if(array_key_exists('field', $cond)) {
$filter .= $cond['field']. ' = ';
}
if(array_key_exists('value', $cond)) {
$filter .= $cond['value'];
}
}
}
$result = $mysqli->query("SELECT ".$fieldString." FROM ".$table. " ".$filter);
$response = $result->fetch_assoc();
if(!empty($response)) {
return $response;
} else {
echo 'response array from get model is empty';
}
} else {
echo '<h3>array data not set for _get() in model </h3>';
}
}
Is the method contained within the class. Sometimes I accidentally put a class function at the end of the class file but outside the ending }. If it is not a class method, remove 'public'.
Change $mysqli->query() to $this->mysqli->query()
I am presuming you created a class method mysqli but you are calling it without '$this'.
If mysqli is the php function, it should be mysqli_query($this->token, "query")
Although, you should be getting ' Undefined variable' with this. In any event $mysqli is undefined in this method.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the PHP error message “Notice: Use of undefined constant” mean?
I got "use of undefined constant" message when load this function:
function awp_get_options($i){
$i[selects][] = 'special_effects';
$i[radios][] = array('js_library','sack');
$i[selects][] = 'effects';
$i[checkboxes][] = 'no_load_library';
return $i;
}
I changed it to:
function awp_get_options($i){
if(isset( $i[selects] )){
$i[selects][] = 'special_effects';
$i[selects][] = 'effects';
}
if(isset( $i[radios] ))
$i[radios][] = array('js_library','sack');
if(isset( $i[radios] ))
$i[checkboxes][] = 'no_load_library';
return $i;
}
It still says use of undefined constant. How to correct this code?
function awp_get_options($i){
$i['selects'][] = 'special_effects';
$i['radios'][] = array('js_library','sack');
$i['selects'][] = 'effects';
$i['checkboxes'][] = 'no_load_library';
return $i;
}
The rest is up to what type is $i given to a function. Other is correct now.
add quotes to your array keys like:
$i[selects]
to
$i["selects"]
//or
$i['selects']
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can't use function return value in write context
This is my source for the variable.
<?php
if ($admin->get_permissions()=3)
echo 'Welcome to the Admin Panel';
else
echo 'Sorry, You do not have access to this page';
?>
And the code that I'm actually trying to call with the if statement is:
public function get_permissions() {
$username = $_SESSION['admin_login'];
global $db;
$info = $db->get_row("SELECT `permissions` FROM `user` WHERE `username` = '" . $db->escape($username) . "'");
if(is_object($info))
return $info->permissions;
else
return '';
}
This should be a simple way to call my pages that the user is authorized for by using an else if statement. Or So I thought
You need to change
$admin->get_permissions()=3
to
$admin->get_permissions()==3
= is the assignment operator, and == is the equality comparison operator. The parser is telling you that you can't assign 3 to the result of a function. It doesn't make any sense to do that.
= != ==
if ($admin->get_permissions() == 3)