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"];

}

?>

 

 

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

 

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

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;
   }
}

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

Archived

This topic is now archived and is closed to further replies.

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