473,322 Members | 1,307 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

tricky problem with setting variables within classes

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
Jul 17 '05 #1
3 2127
Talk about making things hard to make things easy...
Jul 17 '05 #2
Chung Leong wrote:
Talk about making things hard to make things easy...


Thanks for the information... ;-)
Jul 17 '05 #3
Lars Plessmann wrote:
Chung Leong wrote:
Talk about making things hard to make things easy...


Thanks for the information... ;-)


okay. problem solved by myself.
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
8
by: David McDivitt | last post by:
I need to set tabs on java generated pages. Pages have four sections: header, sidebar, body, and footer. The sidebar and body change dynamically. The tab key must go to anchors, fields, and buttons...
25
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
7
by: Nicole | last post by:
Hi I'm trying to use a function to set a session variable. I have three files: The first file has: <?php session_start(); // This connects to the existing session ?> <html> <head>
13
by: Steve Jorgensen | last post by:
== On Error Resume next, and Err.Number == If you want to call one of your procedures from another procedure, and check for errors afterward, you mayimagine that you should write code something...
3
by: Michael Roper | last post by:
Is there a C# approach that allows you to define locals with program lifetime? That is, I don't want to have to expand the scope of a variable just so I can make it persistent. For example,...
5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.