SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Assign $_POST to a variable

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


    TomTees

  2. #2
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    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

  4. #4
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomTees View Post
    I thought "var" was old-school PHP like PHP 3 & 4?

    Do they use it in PHP 5.3?
    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.

    Code php:
    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.

  5. #5
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    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.
    Okay.


    Code php:
    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

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,804
    Mentioned
    157 Post(s)
    Tagged
    3 Thread(s)
    Variables can be assigned an array eg:
    PHP Code:
    $a = array(1,2,3,4,5,6);
    $b $a;

    print_r($b); 
    outputs
    Code:
    Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have an HTML form on one page that launches the code below on my action page...

    Code PHP:
    <?php
    	echo 'FormHandler';
     
    	class FormHandler{
    		private $formValues = array();
     
    		$this->formValues = $_POST;
    	}
    ?>

    NetBeans is complaining about this line...

    PHP Code:
            $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

  8. #8
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomTees View Post
    So using "var" outside of classes is a good idea?
    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.

    PHP Code:
    // 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..

    PHP Code:
    function ( array $post ) {

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


  9. #9
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    try adding [] after $_POST. AAre you expecting large scale deployment?
    Ryan B | My Blog | Twitter

  10. #10
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomTees View Post
    I have an HTML form on one page that launches the code below on my action page...

    Code PHP:
    <?php
    	echo 'FormHandler';
     
    	class FormHandler{
    		private $formValues = array();
     
    		$this->formValues = $_POST;
    	}
    ?>

    NetBeans is complaining about this line...

    PHP Code:
            $this->formValues $_POST
    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.

    PHP Code:
    define('MY_CONSTANT''bar');

    class 
    {
      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 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->$a;
         
    $this->$_POST;
         
    $this->= new Foo();// While legal, not a good habit to form.
         
    $this->3+4;
      }

    Another thing to note, going back to your error.

    Code PHP:
    <?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.

  11. #11
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Boy I have a l-o-n-g way to go...

    Do you know anything about the Factory Pattern?


    TomTees

  12. #12
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    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.

  13. #13
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    o.O Seriously?
    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...


    TomTees

  14. #14
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomTees View Post
    Um, yeah.




    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...


    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.

  15. #15
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  16. #16
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Attached Images Attached Images


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •