tricky problem with setting variables within classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lars Plessmann

    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("f ieldA");

    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.synchroniz e calls the setter function set{$PARAM} of a class
    object, to fill the object with Data

    function synchronize($pa ram, $method="POST")
    {
    $functionName = 'set'.ucfirst($ param);
    if (method_exists( $this, $functionName)) {
    $buf = null;
    if(strtoupper($ method)=="POST" ) {
    $buf = $_POST[$param];
    }
    elseif(strtoupp er($method)=="G ET") {
    $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("n ame",$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($_GE T['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
  • Chung Leong

    #2
    Re: tricky problem with setting variables within classes

    Talk about making things hard to make things easy...


    Comment

    • Lars Plessmann

      #3
      Re: tricky problem with setting variables within classes

      Chung Leong wrote:[color=blue]
      > Talk about making things hard to make things easy...
      >
      >[/color]

      Thanks for the information... ;-)

      Comment

      • Lars Plessmann

        #4
        Re: tricky problem with setting variables within classes

        Lars Plessmann wrote:
        [color=blue]
        > Chung Leong wrote:
        >[color=green]
        >> Talk about making things hard to make things easy...
        >>
        >>[/color]
        >
        > Thanks for the information... ;-)[/color]

        okay. problem solved by myself.

        Comment

        Working...