Need Help....Trying to get property of non-object Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joinnavdev
Forum Newbie
Posts: 21
Joined: Thu Feb 05, 2015 2:34 pm

Need Help....Trying to get property of non-object Error

Post by joinnavdev »

Hi,

I am trying to call getIsAdmin function from another class but getting "Error 500-Trying to get property of non-object"...I am new to PHP so not aware
how to resolve..

I tried to check var_dump also, it's showing "boolean false"

Really looking forward for the help...

Code: Select all

class WebUser
{
    public function getIsAdmin()
    {
        $user = User::model()->findByPk(Yii::app()->user->id);
        return $user->isAdmin == 1;
    }
}

Code: Select all

require_once("WebUser.php");
class ButtonColumn
{
	  function __construct()
     {
		$check= new $WebUser();
		if($check->getIsAdmin()==1)
		{	
			$deleteButtonOptions = array('title' => 'Delete');
			$deleteButtonUrl = 'array("delete", "id" => $data->id)';
		}
	}
}
Thanks,
Nick
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need Help....Trying to get property of non-object Error

Post by Celauran »

Code: Select all

$check= new $WebUser();
Wait, what?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Need Help....Trying to get property of non-object Error

Post by requinix »

Code: Select all

$deleteButtonUrl = 'array("delete", "id" => $data->id)';
That also looks suspicious.
joinnavdev
Forum Newbie
Posts: 21
Joined: Thu Feb 05, 2015 2:34 pm

Re: Need Help....Trying to get property of non-object Error

Post by joinnavdev »

Hi,

First of all thx for your time and help in resolving the issue.

I am just trying to call getIsAdmin function which is another class.....


Below are some images & delete access of records .....are working fine....but I don't want user to have access to delete option...
With admin all is working fine so was trying to call function to check whether the logged one is admin or user....
$deleteButtonUrl = 'array("delete", "id" => $data->id)';..

Also I tried like this
function __construct(WebUser $WebUser) but didn't worked got error..Argument 1 passed to ButtonColumn::__construct() must be an instance of WebUser, instance of GridView given

Hope above information helps..
joinnavdev
Forum Newbie
Posts: 21
Joined: Thu Feb 05, 2015 2:34 pm

Re: Need Help....Trying to get property of non-object Error

Post by joinnavdev »

Hi,

By doing below things I could find admin value......

$user = User::model()->findByPk(Yii::app()->user->id);
echo $user['isAdmin'];........o/p:1

if($user['isAdmin'] == '1')

I am getting below error

Error 500

Trying to get property of non-object......

Any help would be appreciated......

Thanks,
Nick
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Need Help....Trying to get property of non-object Error

Post by Christopher »

In the code you posted, either User::model() or Yii::app() is not returning an object, or Yii::app()->user is not an object.
(#10850)
joinnavdev
Forum Newbie
Posts: 21
Joined: Thu Feb 05, 2015 2:34 pm

Re: Need Help....Trying to get property of non-object Error

Post by joinnavdev »

Christopher,

Thx for helping in resolving the issue.

I got your point but what is the way to resolve it as I am able to get the admin value from database but when I am putting in if statement getting error i.e.Trying to get property of non-object.......

I am new to all this so appreciate your help.

Thanks,
Nick
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Need Help....Trying to get property of non-object Error

Post by Christopher »

It would be helpful to see the complete error message and to tell us what line the error message is referring to. PHP error messages are very clear. Your error message ("Trying to get property of non-object") means you are doing $foo->bar but $foo is not an object. Usually this is because a function/method is returning null or false instead of an object.
(#10850)
Post Reply