Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: function return

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

    Default

    Responding to the last example, that doesn't make much sense:

    Classname::function(.......);
    That's the syntax for calling a static function from a class without an object.

    $variable::function(....);
    That's what you are doing above (even though it's a little more complex), and I don't see any reason this would ever be needed, and it doesn't really make sense: I'm almost positive this would give an error.

    Let's say you have $x that you want to run through Class Z's function Y:
    Z::Y($x);

    Does that make sense?

    Or, you can create an object $o of that class:
    $o = new Z;
    $o->Y($x);

    You can also layer classes (but it can become confusing):
    $p = new W;
    $p->o = $o;
    $p->o->Y($x);

    But you never would use a class and a static function like above.

    Maybe you want to do:
    ClassName::function($obj->subobj);
    ...does that help?

    That is calling a static method on the variable "subobj" (subobject) of class $obj.

    Here's another example. I believe these two are equivalent:
    PHP Code:
    $x = new MyClass;

    //version 1:
    MyClass:MyFunc($x->y);

    //version 2:
    $x->MyFunc($x->y);

    //Or, you can refer to $this->y within the definition for MyFunc.... 
    I hope that makes a little more sense...
    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

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

    Default

    city coder, I don't think you have a clear understanding of what "static" means in our conversation above.

    Your function readCustomer() is not a static function. You will never be able to access it with the syntax Customer::readCustomer().

    In php5, there are four types of methods (functions within a class)(these actually apply to class variables (properties) as well):
    1. public. A "public" function can be accessed by code from outside the class (e.g., from you regular php scripts) after you have created an object from the class. The syntax is like this: $object = new class();, then $object->public_method();. "Public" is the default method type. If you don't declare otherwise (as you are not), then your method will be "public."

    2. protected. Protected methods can be used only by the class itself, and by any class that extend it. This means that the method can be used by your classes, but can't be accessed or triggered directly by any outside code. So, $class->protected_method(); will work if it is a line of code inside the class (or in a class that extends the class), but if you write it in your regular script, it won't.

    3. private. Private methods are like protected methods, except they can only be accessed by their own class (classes that extend the method's class have no access, nor does any outside code).

    4. static. A static method can be accessed from anywhere, even if there is no related object. This is the type that uses the syntax class::static_method();. Static methods are usually utility functions, or other things that you might do frequently but don't need the support of the entire class.
    Edit: They also have no access to the rest of the class (except other static methods, of course) because they aren't working from "inside" the object, even if the object exists. So a static method could not be accessed like this: $object = new class(); $object->static_method();, nor can they access other class functions by using $this->some_non-static_method();. Really, it might as well be a single function written outside the class, but it is a good way to keep related functions organized.


    The rest of it looks functional enough (conceptually; once you finish writing it, of course), but I do have a suggestion.

    It seems that (in this example) you want to use the readCustomer method to "start up" your class. If so, there is a method you could be using called __construct() (two underscores). The __construct method, if present, is run automatically (or, as php.net says, "automagically") when an object is created. So, if you write it like this:
    PHP Code:
    <?php

    class Customer {
       private 
    $fName;
       private 
    $lName;
       private 
    $errorOccurred;
       private 
    $errorText;

       function 
    __construct($email '') {
         
    //  set your SQL statement
         //  if there are results
              //  use class methods to assign the values to the class variables
         //  otherwise, return error message
       
    }
    }

    ?>
    then simply calling $customer = new Customer('test@test.com'); will return an object with all the information you're looking for.
    Edit:

    What's even better (IMHO) is to use the __construct() method to define the logic you want to use to create your object (instructions) rather than actually doing everything. So:
    PHP Code:
    <?php

    class Customer{
       private 
    $name;
       private 
    $email;
       private 
    $whatever;
       private 
    $errorMessage;
       private 
    $queryResults;

       
    // __construct() must always be "public" !!!
       
    public function __construct($email){
          if(
    $this->findCustomerByEmail($email)){
             
    $this->email $email;
             
    $this->setCustomerName();
             
    $this->setCustomerWhatever();
          }else{
             
    $this->setErrorMessage();
          }
       }

       private function 
    findCustomerByEmail($email){
          
    //  use email to search DB and return customer row (if any)
          //  blahblahblah
          
    $this->queryResults $results;
       }

       private function 
    setCustomerName(){
          
    //  use $this->queryResults to set name
          //  maybe something like $this->name = $this->queryResults['name'];
       
    }

       private function 
    setCustomerWhatever(){
          
    //  etc.
       
    }

       private function 
    setErrorMessage(){
          
    //  set error message
          
    if($error){ return $this->errorMessage; }
       }

    }

    ?>
    Last edited by traq; 07-09-2010 at 12:17 AM.

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
  •