By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,309 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,309 IT Pros & Developers. It's quick & easy.

passing $_POST variables to classes

P: n/a
Hi all,

I'm starting this new project in which I'ld like to implement sort of a
design pattern I have seen being used in the CodeIgniter framework.

Basically, the site will examine the URI and based on the segments of the
URI it will fire up some controller class, for instance, say I have an inbox
in which end-users can view messages they got from other users, they'ld
start at:

http://www.somesite.com/inbox/view/

This will then call upon the function view in the class inbox:

class Inbox extends Controller
{
// ...
public function view()
{
// ...
}
// ...
}

Any trailing segments of the URI will be passed as supplement arguments to
the function.

Now, if $_POST variables are sent to some page and are needed by the called
upon function in the appropriate class, would it be bad practice to simply
extract those $_POST variables from the superglobal $_POST inside this
function? Preferably I would like to send the $_POST variables as arguments
to the function, but I'm not sure how to implement this correctly, nor am I
even completely sure as to why this would be better practice, other than
that the function will only accept predefined arguments.

What are your views on implementing such a 'pattern' (if you will). Would
you even suggest such an implementation, or am I bound to get into trouble
with proposed pattern?

Thank you in advance.
Jul 31 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
amygdala wrote:
Hi all,

I'm starting this new project in which I'ld like to implement sort of a
design pattern I have seen being used in the CodeIgniter framework.

Basically, the site will examine the URI and based on the segments of the
URI it will fire up some controller class, for instance, say I have an inbox
in which end-users can view messages they got from other users, they'ld
start at:

http://www.somesite.com/inbox/view/

This will then call upon the function view in the class inbox:

class Inbox extends Controller
{
// ...
public function view()
{
// ...
}
// ...
}

Any trailing segments of the URI will be passed as supplement arguments to
the function.

Now, if $_POST variables are sent to some page and are needed by the called
upon function in the appropriate class, would it be bad practice to simply
extract those $_POST variables from the superglobal $_POST inside this
function? Preferably I would like to send the $_POST variables as arguments
to the function, but I'm not sure how to implement this correctly, nor am I
even completely sure as to why this would be better practice, other than
that the function will only accept predefined arguments.

What are your views on implementing such a 'pattern' (if you will). Would
you even suggest such an implementation, or am I bound to get into trouble
with proposed pattern?

Thank you in advance.

It's better to pass parameters. It makes your class reusable.

What happens, for instance, if later you decide to store the data in the
$_SESSION variable? Or you need to change the name of the $_POST
variable, i.e. due to a conflict? Your existing code would be pretty
worthless.

A compromise would be to have a function which only extracts the data,
but I don't like that.

And you can send the variables - just like you do anything else, i.e.

func($_POST['var'1]);

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 31 '07 #2

P: n/a
Rik
On Tue, 31 Jul 2007 03:23:32 +0200, amygdala <no*****@noreply.comwrote:
Hi all,

I'm starting this new project in which I'ld like to implement sort of a
design pattern I have seen being used in the CodeIgniter framework.

Basically, the site will examine the URI and based on the segments of the
URI it will fire up some controller class, for instance, say I have an
inbox
in which end-users can view messages they got from other users, they'ld
start at:

http://www.somesite.com/inbox/view/

This will then call upon the function view in the class inbox:

class Inbox extends Controller
{
// ...
public function view()
{
// ...
}
// ...
}

Any trailing segments of the URI will be passed as supplement arguments
to
the function.

Now, if $_POST variables are sent to some page and are needed by the
called
upon function in the appropriate class, would it be bad practice to
simply
extract those $_POST variables from the superglobal $_POST inside this
function? Preferably I would like to send the $_POST variables as
arguments
to the function, but I'm not sure how to implement this correctly, nor
am I
even completely sure as to why this would be better practice, other than
that the function will only accept predefined arguments.

What are your views on implementing such a 'pattern' (if you will). Would
you even suggest such an implementation, or am I bound to get into
trouble
with proposed pattern?

There's nothing wrong with using $_POST directly. If however, you're
writing an app that might get it's instructions another way (for instance
the commandline, SOAP, instruction files), it might be handy to let a
controller decide what kind of request it gets, and create a request
object accordingly. When this is used in the code further one, it saves
you having to write the entire interfaces for these 'channels' again, and
assures that all requests are handled in the same manner. Offcourse, the
response for all these channels is usually different, then again 'what
kind of response do I want' i just part of the request offcourse. (Even
when just talking HTTP GETS/POST there might be a difference between calls
for entire HTML pages or for instance JSON objects).
--
Rik Wasmus
Jul 31 '07 #3

P: n/a

"Jerry Stuckle" <js*******@attglobal.netschreef in bericht
news:eu******************************@comcast.com. ..
amygdala wrote:
>Hi all,

I'm starting this new project in which I'ld like to implement sort of a
design pattern I have seen being used in the CodeIgniter framework.

Basically, the site will examine the URI and based on the segments of the
URI it will fire up some controller class, for instance, say I have an
inbox in which end-users can view messages they got from other users,
they'ld start at:

http://www.somesite.com/inbox/view/

This will then call upon the function view in the class inbox:

class Inbox extends Controller
{
// ...
public function view()
{
// ...
}
// ...
}

Any trailing segments of the URI will be passed as supplement arguments
to the function.

Now, if $_POST variables are sent to some page and are needed by the
called upon function in the appropriate class, would it be bad practice
to simply extract those $_POST variables from the superglobal $_POST
inside this function? Preferably I would like to send the $_POST
variables as arguments to the function, but I'm not sure how to implement
this correctly, nor am I even completely sure as to why this would be
better practice, other than that the function will only accept predefined
arguments.

What are your views on implementing such a 'pattern' (if you will). Would
you even suggest such an implementation, or am I bound to get into
trouble with proposed pattern?

Thank you in advance.

It's better to pass parameters. It makes your class reusable.

What happens, for instance, if later you decide to store the data in the
$_SESSION variable? Or you need to change the name of the $_POST
variable, i.e. due to a conflict? Your existing code would be pretty
worthless.
Ah yes, of course, good points. Why didn't I think of that. :)
A compromise would be to have a function which only extracts the data, but
I don't like that.
Sorry, I'm a bit dense today, I can't seem to grasp the simplest of concepts
today, would you care to eleborate on this? Where would this function reside
for instance, and what would it do exactly. Before answering, please view my
comments beneath also, it might save you unnecessary typing. :-)
And you can send the variables - just like you do anything else, i.e.

func($_POST['var'1]);
What I am a bit worried about though (and I know this could be a bit
unlikely), is that when users accidentally (or on purpose), submit a larger
URI (with more segments), my functions will misinterpret the trailing URI
segments, when in fact it should be using $_POST vars.

How would I ensure, giving the fact that trailing URI segments will be
passed to functions as supplement arguments, they will be interpreted as URI
segments and not $_POST data? Any ideas? Thanks!

Or should I perhaps give up on the 'flexible' trailing URI segment stuff and
write some preprocessing code (is this perhaps what you where referring to
when you mentioned a function extracting data?) that will eveluate whether
the URI is acceptable for that particular class? This would somehow seem to
destroy the whole concept I think.
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 31 '07 #4

P: n/a

"Rik" <lu************@hotmail.comschreef in bericht
news:op.twa29wl2qnv3q9@metallium...
On Tue, 31 Jul 2007 03:23:32 +0200, amygdala <no*****@noreply.comwrote:
>Hi all,

I'm starting this new project in which I'ld like to implement sort of a
design pattern I have seen being used in the CodeIgniter framework.

Basically, the site will examine the URI and based on the segments of the
URI it will fire up some controller class, for instance, say I have an
inbox
in which end-users can view messages they got from other users, they'ld
start at:

http://www.somesite.com/inbox/view/

This will then call upon the function view in the class inbox:

class Inbox extends Controller
{
// ...
public function view()
{
// ...
}
// ...
}

Any trailing segments of the URI will be passed as supplement arguments
to
the function.

Now, if $_POST variables are sent to some page and are needed by the
called
upon function in the appropriate class, would it be bad practice to
simply
extract those $_POST variables from the superglobal $_POST inside this
function? Preferably I would like to send the $_POST variables as
arguments
to the function, but I'm not sure how to implement this correctly, nor
am I
even completely sure as to why this would be better practice, other than
that the function will only accept predefined arguments.

What are your views on implementing such a 'pattern' (if you will). Would
you even suggest such an implementation, or am I bound to get into
trouble
with proposed pattern?


There's nothing wrong with using $_POST directly. If however, you're
writing an app that might get it's instructions another way (for instance
the commandline, SOAP, instruction files), it might be handy to let a
controller decide what kind of request it gets, and create a request
object accordingly. When this is used in the code further one, it saves
you having to write the entire interfaces for these 'channels' again, and
assures that all requests are handled in the same manner. Offcourse, the
response for all these channels is usually different, then again 'what
kind of response do I want' i just part of the request offcourse. (Even
when just talking HTTP GETS/POST there might be a difference between calls
for entire HTML pages or for instance JSON objects).
Yes of course, this all makes sense too. And you're probably right, given
the type of requests you mentioned here, they would probably call for
different response types too. And indeed, this wouldn't necessarily mean
that the request interfaces (the arguments) need to be different.

But I have to agree with Jerry's comment (see Jerry's reply) here that if
for instance I decided to use $_SESSION of even $_GET vars for calling my
class functions it would break the interfaces. I did not think of that yet.
--
Rik Wasmus

Jul 31 '07 #5

P: n/a
amygdala wrote:
"Jerry Stuckle" <js*******@attglobal.netschreef in bericht
news:eu******************************@comcast.com. ..
>amygdala wrote:
>>Hi all,

I'm starting this new project in which I'ld like to implement sort of a
design pattern I have seen being used in the CodeIgniter framework.

Basically, the site will examine the URI and based on the segments of the
URI it will fire up some controller class, for instance, say I have an
inbox in which end-users can view messages they got from other users,
they'ld start at:

http://www.somesite.com/inbox/view/

This will then call upon the function view in the class inbox:

class Inbox extends Controller
{
// ...
public function view()
{
// ...
}
// ...
}

Any trailing segments of the URI will be passed as supplement arguments
to the function.

Now, if $_POST variables are sent to some page and are needed by the
called upon function in the appropriate class, would it be bad practice
to simply extract those $_POST variables from the superglobal $_POST
inside this function? Preferably I would like to send the $_POST
variables as arguments to the function, but I'm not sure how to implement
this correctly, nor am I even completely sure as to why this would be
better practice, other than that the function will only accept predefined
arguments.

What are your views on implementing such a 'pattern' (if you will). Would
you even suggest such an implementation, or am I bound to get into
trouble with proposed pattern?

Thank you in advance.
It's better to pass parameters. It makes your class reusable.

What happens, for instance, if later you decide to store the data in the
$_SESSION variable? Or you need to change the name of the $_POST
variable, i.e. due to a conflict? Your existing code would be pretty
worthless.

Ah yes, of course, good points. Why didn't I think of that. :)
>A compromise would be to have a function which only extracts the data, but
I don't like that.

Sorry, I'm a bit dense today, I can't seem to grasp the simplest of concepts
today, would you care to eleborate on this? Where would this function reside
for instance, and what would it do exactly. Before answering, please view my
comments beneath also, it might save you unnecessary typing. :-)
I was referring to have a function which just fetches the data from the
$_POST array. That would be its only purpose. It's a "quick and dirty"
solution - and one I'm not really fond of.
>And you can send the variables - just like you do anything else, i.e.

func($_POST['var'1]);

What I am a bit worried about though (and I know this could be a bit
unlikely), is that when users accidentally (or on purpose), submit a larger
URI (with more segments), my functions will misinterpret the trailing URI
segments, when in fact it should be using $_POST vars.

How would I ensure, giving the fact that trailing URI segments will be
passed to functions as supplement arguments, they will be interpreted as URI
segments and not $_POST data? Any ideas? Thanks!

Or should I perhaps give up on the 'flexible' trailing URI segment stuff and
write some preprocessing code (is this perhaps what you where referring to
when you mentioned a function extracting data?) that will eveluate whether
the URI is acceptable for that particular class? This would somehow seem to
destroy the whole concept I think.
Two choices. You can pass the entire URI to the class and strip off
what you need, or you can strip off what you don't need and pass the
rest to your class.

But either way you'll have to validate the URI to ensure it is valid.
It's just a matter of do you do it in the class or in the code calling
your class method.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 31 '07 #6

P: n/a
On Jul 30, 9:23 pm, "amygdala" <nore...@noreply.comwrote:
Hi all,

I'm starting this new project in which I'ld like to implement sort of a
design pattern I have seen being used in the CodeIgniter framework.

Basically, the site will examine the URI and based on the segments of the
URI it will fire up some controller class, for instance, say I have an inbox
in which end-users can view messages they got from other users, they'ld
start at:

http://www.somesite.com/inbox/view/

This will then call upon the function view in the class inbox:

class Inbox extends Controller
{
// ...
public function view()
{
// ...
}
// ...

}

Any trailing segments of the URI will be passed as supplement arguments to
the function.

Now, if $_POST variables are sent to some page and are needed by the called
upon function in the appropriate class, would it be bad practice to simply
extract those $_POST variables from the superglobal $_POST inside this
function? Preferably I would like to send the $_POST variables as arguments
to the function, but I'm not sure how to implement this correctly, nor am I
even completely sure as to why this would be better practice, other than
that the function will only accept predefined arguments.

What are your views on implementing such a 'pattern' (if you will). Would
you even suggest such an implementation, or am I bound to get into trouble
with proposed pattern?

Thank you in advance.
The request registry might be a good way to abstract the request data
in your case. The registry will handle the data gathering from your
superglobals (and possibly other places if you wish) and provide a
central point to references the data, without having to pass the
variables around your API.

http://www.phppatterns.com/docs/design/the_registry

Jul 31 '07 #7

P: n/a
amygdala wrote:
Now, if $_POST variables are sent to some page and are needed by the called
upon function in the appropriate class, would it be bad practice to simply
extract those $_POST variables from the superglobal $_POST inside this
Passing parameters as global variables is fully idiotic way. Never do this.
function? Preferably I would like to send the $_POST variables as arguments
to the function, but I'm not sure how to implement this correctly, nor am I
even completely sure as to why this would be better practice, other than
that the function will only accept predefined arguments.
Better practice: pass to function only needed parameters, not all
$_POST array. Test parameters types in function.
In all my code I have '$_GET' string only 1 (one) time and '$_POST'
string only 1 (one) time, in my utils::getArgs() function. And only page
component uses it and then pass needed data to child components.

/// Get script arguments
/**
* Depends on calling method.
* Suports GET, POST and command line calling.
* Removes magic_quotes_gps effect
*/
static function getArgs()
{
if(isset($_SERVER['REQUEST_METHOD']))
{

if($_SERVER['REQUEST_METHOD']=='POST')
$return=$_POST;
else $return=$_GET;

if(get_magic_quotes_gpc())

array_walk_recursive($return,create_function('&$va lue','$value=stripslashes($value);'));
return $return;
}else if(isset($_SERVER['CMDLINE']))
{
include_once 'parser.class.php';
$parser=new Parser($_SERVER['CMDLINE']);
$parser->readRegexp('[^ ]+([ ]+-[^ ]+)*[ ]+[^ ]+');
$retval=array();
while(strlen($parser->buffer))
{
if($parser->skipSpace()) continue;

if(!$retval[]=$parser->readRegexp('(("[^"]*")|[^ "]+)'))
break;
}
return $retval;
}
return array();
}
Aug 1 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.