Reputation: 167
Probably a newbie question, but then I'm learning on the fly:
Within the following code, I need to name $targetname and $imagelocation come from the $_POST variable coming in... I KNOW I can't define these variables properly the way I'm trying to, but am a bit stumped... help anyone?
class PostNewTarget{
//Server Keys
private $access_key = "123456";
private $secret_key = "142356";
private $targetName = $_POST['the_target'];
private $imageLocation = $_POST['the_image'];
function PostNewTarget(){
$this->jsonRequestObject = json_encode( array( 'width'=>300, 'name'=>$this->targetName , 'image'=>$this->getImageAsBase64() , 'application_metadata'=>base64_encode($_POST['myfile']) , 'active_flag'=>1 ) );
$this->execPostNewTarget();
}
...
Upvotes: 1
Views: 4755
Reputation: 1050
Treat it as a normal variable and put it in a constructor or mutator or argument of a function if it's only needed in that function. Here is an example using a constructor, but the logic is the same in every case.
class Example {
public $varFromPOST;
public $name
public function __construct($var, $name) {
$this->varFromPOST = $var
$this->name = $name
}
}
Then in your index.php:
$_POST['foo'] = 100;
$userName = 'Bob';
$Example = new Example($_POST['foo'], $userName);
Seems straightforward, if I didn't misunderstand your question.
Upvotes: 2
Reputation: 4446
You need to initialize first your class properties
You can set $_POST values to your class properties when you create your class object using constructor, or you can set just when you need to get these values, I made it in your example
class PostNewTarget{
//Server Keys
private $access_key = "123456";
private $secret_key = "142356";
private $targetName = "";
private $imageLocation = "";
//you can give class variables values in the constructor
//so it'll be setted right when object creation
function __construct($n){
$this->targetName = $_POST['the_target'];
$this->imageLocation = $_POST['the_image'];
}
function PostNewTarget(){
//or you set just only when you need values
$this->targetName = $_POST['the_target'];
$this->imageLocation = $_POST['the_image'];
$this->jsonRequestObject = json_encode( array( 'width'=>300, 'name'=>$this->targetName , 'image'=>$this->getImageAsBase64() , 'application_metadata'=>base64_encode($_POST['myfile']) , 'active_flag'=>1 ) );
$this->execPostNewTarget();
}
}
Upvotes: 2
Reputation: 78994
Pass into the method:
function PostNewTarget($targetName, $imageLocation)
Then call with:
PostNewTarget($_POST['the_target'], $_POST['the_image'])
You could possibly add in the constructor, but I wouldn't:
public function __construct() {
$this->targetName = $_POST['the_target'];
$this->imageLocation = $_POST['the_image'];
}
Upvotes: 3