Jump to content

why should using $_POST[] in a Class create an error ?


vincej

Recommended Posts

I'm new to OOP and straight out the gate Dreamweaver keeps giving me an error which I do not understand. I get that $_POST creates an array, however, in the procedural world I have always been able to assign a $variable to a $_POST. In fact if I remove the Class declaration the error goes away. What am am I missing ? - Many Thanks VJ :

 

<?php

class LookUp { 	
$first_name = $_POST["f_name"];
$last_name = $_POST ["l_name"];
$company = $_POST["company"];

}

?>

 

 

Link to comment
Share on other sites

Thanks - Dumb mistake, but adding the 'PUBLIC'  declaration still has not made any difference to Dreamweaver still complaining. Here is what I got:

 

<?php

class LookUp { 	
public $first_name = $_POST["f_name"];
public $last_name = $_POST ["l_name"];
public $company = $_POST["company"];

}


?>

 

Many thanks for any ideas - VJ

 

Link to comment
Share on other sites

HI - DW says

 

" There is a syntax error on line 4. Code hinting may not work until you fix this error"

 

 

line 4 is the first variable line ... but all the variable lines have a red mark on the margin line numbers !

 

Many thanks VJ

Link to comment
Share on other sites

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

 

You should probably set them in a constructor:

 

class LookUp { 	
   public $first_name;
   public $last_name;
   public $company;

   function __construct() {
      $this->first_name = $_POST["f_name"];
      $this->last_name = $_POST ["l_name"];
      $this->company = $_POST["company"];
   }
}

 

I don't know what you're doing but this may not be the best idea because now your class is dependent on POST vars being available.  Maybe more like this:

 

class LookUp { 	
   public $first_name;
   public $last_name;
   public $company;

   function __construct($f_name, $l_name, $company) {
      $this->first_name = $f_name;
      $this->last_name = $l_name;
      $this->company = $company;
   }
}

Link to comment
Share on other sites

I am wanting to pull in form data and INSERT to Mysql. I can do this procedurally but I am really wanting to make the transition to OPP. I need the script to run automatically when I hit the submit button and action=look_up.php

 

You a star - thank you.

 

VJ

Link to comment
Share on other sites

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.