Jump to content

What are some modern ways to instantiate classes for polymorhism?


Recommended Posts

Is this an acceptable way to instantiate classes?

//$_SESSION['product'] = {'ProductA', 'ProductB', ... 'ProductX'} 

$p = new $_SESSION['product']();

$p->save();

I am usually used to calling out classes explicitly where class name is not a variable but a hardcoded string.  Sometimes I use if/then/else in order to do this. Here it is a variable and it bothers me a little bit.   But PHP allows me to do this.

Is this an acceptable latest & gratest modern PHP object oriented web technology technique or not ?

Unless you restrict the possible classes, this is a gigantic security vulnerability. The session values are not reliable and may have been injected by the user (this shouldn't happen in a properly written application, but it does). If you let your users instantiate arbitrary classes and call their methods, you're in deep trouble.

 

So, no, you can't just put the new operator in front of some input parameter. You need to actually check the class name before you create an instance.

Edited by Jacques1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.