Assign $_POST to a variable

How do you assign the entire $_POST array to a variable (array)?

TomTees

var $a = $_POST;

or even

$a = $_POST;

The difference between the two statements is that the first statement formally initializes $a, and assigns it while the second assigns to $a regardless of whether or not it is defined. That said PHP as a language doesn’t care much (it raises a notice) if you don’t declare a variable before using it, but most languages do and it’s a good habit to develop for later on when you move to those languages.

I thought “var” was old-school PHP like PHP 3 & 4?

Do they use it in PHP 5.3?

Also, is there a way to define variable types ahead of time?

Type-hinting maybe?

I’m used to this style of coding…

$name string
$age integer
$children array()

$name = Robert
$age = 27
$child1 = $children[1];
$child2 = $children[2];

TomTees

var is still to be used when declaring a variable anywhere BUT as a member of a class, but when declared as a member of a class it is to be dropped.


class A {
  private $a = 1;
  protected $b = 2;
  public $c = 3;
  var $d = 4; // Deprecated, scope will be public.
}

var $d = new A();  // Not deprecated, nor will be anytime soon.

Also, is there a way to define variable types ahead of time?

Type-hinting maybe?

I’m used to this style of coding…

On my wish list, but at present no. Nor does type hinting work for anything other than objects or arrays.

Okay.


class A {
  private $a = 1;
  protected $b = 2;
  public $c = 3;
  var $d = 4; // Deprecated, scope will be public.
}

var $d = new A();  // Not deprecated, nor will be anytime soon.

So using “var” outside of classes is a good idea?

On my wish list, but at present no. Nor does type hinting work for anything other than objects or arrays.

But $_POST is an array?

How would you use type-hinting with $_POST?

TomTees

Variables can be assigned an array eg:


$a = array(1,2,3,4,5,6);
$b = $a;

print_r($b);

outputs


Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )

I have an HTML form on one page that launches the code below on my action page…

<?php
	echo 'FormHandler';

	class FormHandler{
		private $formValues = array();

		$this->formValues = $_POST;
	}
?>

NetBeans is complaining about this line…

		$this->formValues = $_POST;

And if I populate all fields in my form and click ‘Submit’ I get this error…

( ! ) Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /Users/user1/Documents/DEV/++htdocs/Ecommerce/02_FormHandler.php on line 7

I’m just trying to do simple tests to get comfortable with how things work…

TomTees

Yes. It makes the formal declarations of the variable more visible, and it helps some IDE’s in examining your code. Also, as in most languages you can separate with a comma.


// ok.
var $a = 3, $b = false, $c = 'foo';

// also ok, but harder to spot.
$d = array();

// Set error reporting to all because
// Notices don't normally show and
// one is needed for next example.
error_reporting(-1);

if ($e) { echo 'true'; }

// PHP issues a Notice because $e was not 
//set before being examined. It will be set 
//immediately with a value of null.

But $_POST is an array?

How would you use type-hinting with $_POST?

$_POST is indeed an array, even when empty. It is one of 6 major super global variables PHP defines automatically. The others are

$_REQUEST – bad idea to ever use this - pretend it doesn’t exist.
$_GET – Get parameters.
$_COOKIE – Cookies, yum
$_SERVER – Server variables
$ENV – OS Environment
$GLOBALS – Array of all variables in the global scope. Avoid using whenever possible.

Superglobal means these vars are in the scope of all functions at all times.

So yes, you can take post as an argument and even type hint like this…


function ( array $post ) {

  // But you can see $_POST anyway even if its not an argument.
  print_r($_POST);
}

try adding after $_POST. AAre you expecting large scale deployment?

PHP doesn’t allow you to assign values to a class method at compile time - that is when the script is first read in - unless those values come from a constant.


define('MY_CONSTANT', 'bar');

class A {
  protected $a; // legal, $a will start null.
  protected $b = 3; // legal, 3 is a numeric constant.
  protected $c = MY_CONSTANT; // legal, $c will be 'bar';
  protected $d = 3 + 4; // error - can't calculate at compile time.
  protected $e = array(); // legal
  protected $f = new Foo(); // error, function return can't be calc'ed at compile.
  protected $g = $a; // error - $a is a variable.
  protected $h = $_POST; // Illegal, $_POST is also a variable.

  /**
    * The construct function allows us to populate member vars
    * when the class is intialized.
    */
  public function __construct( $a ) {
     $this->g = $a;
     $this->h = $_POST;
     $this->f = new Foo();// While legal, not a good habit to form.
     $this->d = 3+4;
  }
}

Another thing to note, going back to your error.

<?php
	class FormHandler{
		private $formValues = array();
		$this->formValues = $_POST;
	}
?>

$this->formValues must be within a function of the class. You sometimes see this sort of thing in Javascript, but that’s because “classes” in javascript are just function objects that have other function objects, and statments in the body of the “class” are the body of the function. PHP won’t allow it, nor will most languages.

Boy I have a l-o-n-g way to go… :blush:

Do you know anything about the Factory Pattern?

TomTees

o.O Seriously?

My brain is still hurting from my last debate about it over in Applications. It’s still close to the top of the thread list if you want a look.

Um, yeah.

My brain is still hurting from my last debate about it over in Applications. It’s still close to the top of the thread list if you want a look.

Oh.

My original post was an attempt to break down some code iw as given dealing with the Factory Pattern that I have not been able to understand in 3 days…

I was hoping to post some more code here and maybe get some help understanding what is going on… :frowning:

TomTees

I’ll sum up what’s over there. The basic premise of Factory pattern is that objects aren’t instantiated explicitly. That is, you never use

$a = new A();

Instead you use

$a = ‘A’
$b = new $a();

The idea here is that with the class of $b now a variable it can be changed as needed. The code that does this assignment and reassignment of classes to use for an object is managed in a “factory” object. Some factory patterns are so elaborate that the factories have factories.

That’s the 50 word summary, but the goal of the pattern is flexibility. The pitfall of the pattern is overkill. Some objects are so primitive that they don’t necessarily need factories. It’s a judgement call as to when this occurs though.

Factories are also one way to combat dependency.

One day I’ll write a book on this sort of thing I think that tries to aim this topic towards beginning and novice programmers. PHP has many excellent beginner manuals, and many great expert ones, but the middle ground feels lost to me, of the books I’ve read.

Thanks for the info but I really need help understanding the code I was given.

If you are game and I can tack it on to this thread then I’d welcome the help!

TomTees

Attached is a Class Diagram that I am trying to understand…

In FormCollection:

  • FormCollection is an abstract class?
  • $fields is an array that holds the $_POST array which contains all form values?

In RegistrationForm:

  • $formInputs is an array of FormInput objects?

In FormInput:

  • FormInput is an abstract class?
  • FormInput is a ‘Factory Class’?

TomTees