Problem: I try to store data in a objects field and read it out again.
Sounds easy, yeah. But its a bit tricky here.... ;-)
This is the class Customer.php with some setter and getter functions to
store customers data within an object.
It works.
Furthermore, it includes a synchronize method, which calls the
individual setXXX function and takes the data
from the $_POST or $_GET var.
Therefore the fieldnames from the HTML form and objectfields have to be
equal, so that data for $obj->fieldB
can be found at $_GET['fieldB']. Well, no problem, yet.
sample:
$obj->synchronize("fieldA");
will call the function setFieldA($_GET/POST['fieldA']) from
the objects methods
So this is the Customer.php (shortened to the basics we might need here):
--CODE-----------------------------------------------
...
// setter and getter of a class customer
function getName()
{
return $this->name;
}
function setName($name)
{
if(strlen($id) <= 40) {
$this->name = $name;
return true;
}
else {
return false;
}
}
...
// $obj.synchronize calls the setter function set{$PARAM} of a class
object, to fill the object with Data
function synchronize($param, $method="POST")
{
$functionName = 'set'.ucfirst($param);
if (method_exists($this, $functionName)) {
$buf = null;
if(strtoupper($method)=="POST") {
$buf = $_POST[$param];
}
elseif(strtoupper($method)=="GET") {
$buf = $_GET[$param];
}
if($buf <> null) {
return call_user_func( array($this, $functionName), $buf );
}
else {
return false;
}
}
else {
return false;
}
}
...
-----------------------------------------------------
Well. The class Customer.php itself works!
But now, I try to send data to the object via the synchronize function.
To do this, I use the following sample script, which receives data via
method GET by URL as you can see:
.../step2.php?name=JohnDoe
--CODE-----------------------------------------------
<?php
include_once 'Customer.php';
$newCust = new Customer();
$method = "GET";
if (!$newCust->synchronize("name",$method)) {
printError( "Der Name ist nicht korrekt!");
};
print "Result: ".$newCust->getName();
?>
------------------------------------------------------
Well thats all.
It should send "JohnDoe" to the $newCust->name variable and print out:
Result: JohnDoe
But in fact, it prints: Result:
To make it more clear what I need:
The expected result could be reached by doing the following from the
step2.php:
$newCust->setName($_GET['name']);
But I have so many fields and forms which change often, that this is not
an effective way!
I need to do this via my synchronize method, that calls the
corresponding setXXX function automatically.
Well, the call of the function in the class itself works, but I cannot
read the object's field at the end!
I tried putting a >>print "test: $name"<< within the setName function
and it seems to work correctly!
So the value is sored somewhere, but I don'T know where it is stored!
Maybe I write it to a wrong new object? is $this wrong? do I need to
point to $parent?
I hope anybody can help me!?
Thanks in advance,
Lars