check instance of an object

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
paraleadogg
Forum Newbie
Posts: 11
Joined: Wed Feb 11, 2009 12:16 pm

check instance of an object

Post by paraleadogg »

Hi guys

I am a noob and wondered if someone could explain something to me

I have a user class

And a developer class that inherits from the user class.

I also have an item class with a method to "create item"

Only a developer can create items.

So - I want to pass my developer instance to my item->createitem() method

Code: Select all

 
$dev = new developer();
$item = new item();
$item->createitem($dev)
 
Then within the create item method I need to check that $dev IS an instance of a developer - and not just a user

So something like this

Code: Select all

 
createitem($dev)
{
if ($dev is developer)
{
       create item;
}
else
{
       not allowed;
}
}
 
Any help appreciated
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: check instance of an object

Post by Mark Baker »

Code: Select all

 
createitem($dev)
{
if ($dev instanceof developer)
{
       create item;
}
else
{
       not allowed;
}
}
 
Any help appreciated[/quote]
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: check instance of an object

Post by John Cartwright »

I think it should be the other way around, and you pass the item object to the developer object to be created, i.e.

$developer = new Developer();
$developer->createItem(new Item());

Reguardless, you can check the instance of the object using "instanceof"

Code: Select all

function createitem($dev)
{
  if ($dev instanceof developer)
  {
       //create item;
  }
  else
  {
       //not allowed;
  }
}
paraleadogg
Forum Newbie
Posts: 11
Joined: Wed Feb 11, 2009 12:16 pm

Re: check instance of an object

Post by paraleadogg »

Aaah I thought so - thans John.

By the way - the thinking behind passing the developer to the item and not the other way round was just logical - to keep the item methods within the item class (create, delete, update etc)

No other reason

John Cartwright wrote:I think it should be the other way around, and you pass the item object to the developer object to be created, i.e.

$developer = new Developer();
$developer->createItem(new Item());

Reguardless, you can check the instance of the object using "instanceof"

Code: Select all

function createitem($dev)
{
  if ($dev instanceof developer)
  {
       //create item;
  }
  else
  {
       //not allowed;
  }
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: check instance of an object

Post by John Cartwright »

paraleadogg wrote:Aaah I thought so - thans John.

By the way - the thinking behind passing the developer to the item and not the other way round was just logical - to keep the item methods within the item class (create, delete, update etc)
The item object should have no responsibility in knowing who is using itself. It is the responsibility of the developer or user class to determine whether they have access to using items.

Now, you can still keep all your methods in the item class, but the developer class should support some sort of composition to add items. I've added an example below, although I did use a fluent intertace to improve it's usability.

Code: Select all

 
 
$developer = new Developer();
 
$developer->item(new Item())->create();
//or
$developer->item(new Item())->update();
//etc
 
class Developer 
{
   protected $_item;
 
   function item(Item $item)
   {
      $this->_item = $item;
      return $this->_item;
   }
}
 
class Item 
{
   public function __construct() {}
 
   public function create()
   {
       //do create item stuff
       return $this;
   }
}
 
 
paraleadogg
Forum Newbie
Posts: 11
Joined: Wed Feb 11, 2009 12:16 pm

Re: check instance of an object

Post by paraleadogg »

John Cartwright wrote:
The item object should have no responsibility in knowing who is using itself. It is the responsibility of the developer or user class to determine whether they have access to using items.

Now, you can still keep all your methods in the item class, but the developer class should support some sort of composition to add items. I've added an example below, although I did use a fluent intertace to improve it's usability.

Code: Select all

 
 
$developer = new Developer();
 
$developer->item(new Item())->create();
//or
$developer->item(new Item())->update();
//etc
 
class Developer 
{
   protected $_item;
 
   function item(Item $item)
   {
      $this->_item = $item;
      return $this->_item;
   }
}
 
class Item 
{
   public function __construct() {}
 
   public function create()
   {
       //do create item stuff
       return $this;
   }
}
 
 
Hi John,

Developer inherits from User. So if i wanted developers and basic users to have access to different methods within the Item class I could do this

Code: Select all

 
$developer = new Developer();
 
$user = new User();
 
$developer->parent::item(new Item())->create();
//or
$developer->parent::item(new Item())->update();
//etc
$user->item(new Item())->view();
 
 
class Developer extends User
{
 
}
 
class User
{
  protected $_item;
 
   function item(Item $item)
   {
      $this->_item = $item;
      return $this->_item;
   }
}
 
class Item 
{
   public function __construct() {}
 
   public function create()
   {
       //do create item stuff
       return $this;
   }
} 
Is that right?

But then surely that means that a basic User could also access create? delete? etc

A basic user should not be able to access these methods
Last edited by paraleadogg on Wed Mar 25, 2009 12:53 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: check instance of an object

Post by John Cartwright »

It's a bit dificult to judge exactly how to handle your situation, since I'm looking at your code out of context.

Indeed if you want grained access control the example I've given would not be suitable.

Personally, I would create implementations of your Item object for each users access controls. I.e.

Code: Select all

 
//no access controls
class DeveloperItem extends Item { } 
 
//access controls
class UserItem extends Item {
   public function create() {
      throw new Exception('UserItem cannot implement Item::create()');
   }
 
   public function update() {
      throw new Exception('UserItem cannot implement Item::update()');
   }
}
 
and maybe even a factory to return the appropriate child item depending on users role. The point is that the Item class should not be concerned about it's own implementation.

But like I say this may not be ideal for your situation.
paraleadogg
Forum Newbie
Posts: 11
Joined: Wed Feb 11, 2009 12:16 pm

Re: check instance of an object

Post by paraleadogg »

Hi John,

Basically a developer inherits from User. So if i wanted developers and basic users to have access to different methods within the Item class I could do this

Code: Select all

 
$developer = new Developer();
 
$user = new User();
 
$developer->parent::item(new Item())->create();
 
$developer->parent::item(new Item())->update();
 
$user->item(new Item())->view();
 
 
class Developer extends User
{
 
}
 
class User
{
  protected $_item;
 
   function item(Item $item)
   {
      $this->_item = $item;
      return $this->_item;
   }
}
 
class Item 
{
   public function __construct() {}
 
   public function create()
   {
       //do create item stuff
       return $this;
   }
} 
But then a basic user would also have access to methods they shouldnt have (like create, update etc) wouldnt they?

Thanks
Post Reply