Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: function return

  1. #1
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default function return

    hi guys is it possible to do something like this??

    PHP Code:
    class {
       private 
    $test;
       private 
    $anotherTest;
       
       function 
    getTest() {
          return 
    $this->test;
       }

       function 
    setTest($value '') {
          
    $this->test value;
       }

       function 
    getAnotherTest() {
          return 
    $this->anotherTest;
       }

       function 
    setAnotherTest($value '') {
          
    $this->anotherTest value;
       }
    }

    class 
    {
        private 
    $variable;

        function 
    getVariable() {
           return 
    $this->$variable;
        }

        function 
    setVariable($value '') {
           if(
    $value != '') {
                
    $a = new A();
                
    $a->setAnotherTest($value);

                
    $return $a;
           } else {
                
    //do something else
           
    }

    PHP Code:
    $b B::setVariable('text');
    echo 
    $b->getAnotherTest(); 
    essentially i want to know whether i can pass a class in a return statement or you really just cant & it has to be as an array?

    cheers in advance guys.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm not sure I understand exactly what you are trying to accomplish. Can you make a simpler example? There's a lot going on there that makes this confusing.
    You should be able to return any variable regardless of its contents. Returning a variable just creates a copy of that variable in the other context... there should be no real problems.

    The only thing that jumps out at me in your code is that you have a type: $return instead of return. "$return" is a variable named return, not a command.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    one other thing:
    you are trying to call B::setVariable(), but it's not a static method. Choose one of two solutions:
    PHP Code:
    <?php
    $b 
    = new B();
    $b->setVariable();
    ?>
    or
    PHP Code:
    <?php
    class B{
    //  ...
    static function setVariable(){
       
    //  ...
    }
    //  ...
    }
    ?>
    try that and see where it takes you. To answer your question ("can you return a class (actually, the object the class created)"), yes, but beyond being kinda interesting, I've rarely found it to be useful.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Also, since there are clearly some errors in this script, seeing those will help us. If you see no errors (blank screen?) then this means you need to turn on error reporting so you can diagnose the problems. There is information available by search for "php error reporting" and on php.net for the function error_reporting().
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Also, since there are clearly some errors in this script, seeing those will help us. If you see no errors (blank screen?) then this means you need to turn on error reporting so you can diagnose the problems. There is information available by search for "php error reporting" and on php.net for the function error_reporting().
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    Sorry for the confusing example guys....il make it more simple i promise :P

    heres the scenario, i send a function in a class a parameter. The function then connects to the database (which as you probably noticed was my other thread about a database class, but thats besides the point) & returns a row from the table based on the data. So hopefully this is easier to understand....

    (btw, this is not my code im using exactly, this is a cut down version)
    PHP Code:
    class Customer {

       var 
    $name;
       var 
    $email;

    function 
    getEmail () {
       return 
    $this->email;
    }

    function 
    setEmail ($value '') {
       
    $this->email $value;
    }

    //etc etc for getting & setting

    function readCustomer ($value '') {
       
    //$value will be an email address
       
    $sql "SELECT * FROM customer WHERE email = '$value'";
       
    $result mysql_query();
       
    $customer = new Customer();
       if(
    //results greater than 0) {
          
          
    $customer->setEmail //row from table
          
    $customer->setName //row from table

       
    } else {
          
    $customer->setEmail null;
          
    $customer->setName null;
       }

       return 
    $customer;
    }


    then to access it....

    PHP Code:
    $customer = new $customer::read('test@test.com');
    echo 
    $customer->getEmail();
    echo 
    $customer->getName(); 
    The only reason i wanted to return a different type of object was cos i want to return the data in an object & a response message that i would then use to check whether any results came back & i could set error flags & messages etc.

    I hope that explains things a little better.

    Cheers guys
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  7. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    again, yes, you can return an object. However, if you only need the data/messages/etc., it would be much simpler and more efficient to use an array. There's no reason to return an object unless you actually need to manipulate the object (e.g., you're going to use the object's functions) after it's returned.

    also, your sample code above will still not work. If you want to use an object's methods outside the object (e.g., from your regular script), you need to declare them as static methods in the class definition. Otherwise, you won't be able to use them from the outside: you'll have to create a new object from the class and use that to access the methods.

    (BTW, based on the way your code is written in your example, this:
    $customer = new $customer::read('test@test.com');
    should be something like this:
    $customer = new customer();
    $customer->readCustomer('test@test.com');

    )

    If this is one of your first attempts at using object-oriented programming, I'd suggest doing something much simpler to start. Find some tutorials you can understand and do lots of experimenting. It took me quite a while to figure out OOP; and it doesn't work quite the way you might expect it to.

    Edit:

    This is not correctly formed, either:
    PHP Code:
    <?php
    // ...
    if(//results greater than 0) {
          
          
    $customer->setEmail //row from table
          
    $customer->setName //row from table

       
    } else {
          
    $customer->setEmail null;
          
    $customer->setName null;
       }
    // ...
    ?>
    $customer->setEmail (et.al.) are not variables; they're methods. You can't set them the way you set variables.
    try something like:
    PHP Code:
    <?php

    if( /*  $results = results from DB*/ ){
       
    $customer->setEmail($results['email']);
       
    $customer->setName($results['name']);
    }else{
       
    //  you don't have to "set" the values as null; they're already empty to start.
       //  setting another property might be more helpful:
       
    $customer->setFoundCustomer(FALSE);
       
    //  or even:
       //  return FALSE;
       //  instead of returning the customer object
    }

    ?>
    Last edited by traq; 07-05-2010 at 07:07 PM.

  8. #8
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    OK, so its not my first attempt at using OOP. Iv been doing it in java for 3 months in SAP so I know how it works there. Iv just wanted to do this project in PHP because I know it better than moving to JSP & learning that more.

    So you said it is possible to return an object but you didnt say how, which is what my question was.

    Also you didnt give any examples of how that code was structured in the class & what was set.

    If you can give me some examples please? That would be a great help!

    Cheers.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  9. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    well, PHP is still different than Java. if you're already familiar with OOP, then you're ahead of the game; perhaps this would be helpful with sorting out php-specific syntax and constructs.

    about examples: sorry - I thought you were just asking if what you had would work and how it would work. Aside from the typos and preceding syntax issues, the "return" part of your code was fine. An object is returned the same way anything else is:
    PHP Code:
    <?php

    function return_object(){
       
    $object = new myClass();
       return 
    $object;
    }

    ?>
    To declare an object as static (so you can access it like $var = myClass::static_method(); ), just put the static keyword in front of it in the class definition:
    PHP Code:
    <?php

    myClass
    {

       static function 
    static_method($var){
          
    //  do stuff
       
    }

    }

    ?>
    Last edited by traq; 07-05-2010 at 07:12 PM.

  10. #10
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    Sorry its taken so long to reply...

    Cheers for the response. So just to double check this now, if i do...

    PHP Code:
    class Customer {
       private 
    $fName;
       private 
    $lName;
       private 
    $errorOccurred;
       private 
    $errorText;

       function 
    setLName($value '') {
          
    $this->lName $value;
       }

       function 

       function 
    readCustomer ($email '') {
          
    $sql ="SELECT * FROM customer WHERE email = '$email'";
          
    $customer = new Customer();
          if 
    result then {
             
    $customer->setLName($result['lName']);
             
    //etc
          
    } else {
               
    //set flag same as setting lName
          
    }

          return 
    $customer;
       }

    then to read it

    PHP Code:
    $customer = new Customer();
    $customer->readCustomer('test@test.com');

    if(
    $customer->getErrorOccurred()) {
       
    //error couldnt get user details from db
    } else {
       
    //great, get other customer details
       
    echo $customer->getLName();


    right? or wrong?

    also could i do this?

    PHP Code:
    $customer->Customer::readCustomer('test@test.com'); 
    or does that mean im doing things differently?

    cheers for the help guys, i know im being stupid.
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •