SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: A question.

  1. #1
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A question.

    This is my first post in this Advanced PHP forum. I've been lurking for some time but never felt I had anything to offer and my questions have been unformed due to lack of knowledge. Well, I'm starting to understand this whole OOP using classes aproach to coding. I can see how it would have saved me so much time in the past. Time is something I have too little of. So I have been messing around writing some primitive classes based on my previous code. Believe it or not it's all worked so far.

    Anyway, a question I have is, can a function that is within a class be used to manipulate data in another function in that class? If so how? I am starting to see that I have similar functions to perform within different functions and I don't want to create repetitive code while trying to eliminate the same.

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you asking if you can do something like this?
    PHP Code:
    class foo
    {
      var 
    $data;

      function 
    foo($val)
      {
        
    $this->data $this->validate($val); 
      }

      function 
    validate($val)
      {
        return (
    strlen($val)>2) ? $val false;
       }

      function 
    bar($val)
      {
        if (
    $this->validate($val)) {
          echo 
    "$val is okay";
        } else {
          echo 
    "$val not okay, try ".$this->data;
        }
      }

    ?

    Edit:

    to be more clear, anything data you want to share across multiple methods (functions) should be declared as class vars.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure if that's what I'm asking. My code is probably not that sophisticated yet. Basically, right now I have a function that I have used to color output based on what it is. It's just a switch in a function. Now I can include the basic switch in each function in the class that has output (should I be calling them methods here? ) but that seems repetitive. I was just wondering if I can just add the function to the class and use it to manipulate the data within other functions in that class.

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you should only code the switch once. Have the switch work off of a class var, and then when you need the color, use $this->SelectColor() or whatever.

    Incidentally, until we get to PHP5, there is a convention you should probably adopt. If you intend the method only to be used internally to the class, prefix it with and _ i.e. $this->_SelectColor(). This is what is referred to as a "private method".

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depending on your design there are a few options:

    1) Pass the object with the switch to the object that uses it:
    PHP Code:
    class Object1 {
        var 
    $switchObj;
        function 
    Object1(& $obj) {
            
    $this->switchObj =& $obj;
        }
        function 
    doSomething () {
            
    $this->switchObj->switchFunction();
        }

    2) Make objects that use the switch an extension of the switch object, they then gain all functions from their parent and you call them like $this->switchFunction(); But don't go around freely making every object an extension, it's just not good design.

    3) If you solely want to call the function and the function doesn't rely on other parts of the object, you can do this:
    PHP Code:
    object_name::switchFunction(); 
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.

  6. #6
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ouch my head just poped. Obviously there is a lot I need to learn to even understand half of that. heh. Plus I'm real tired from overwork. I'll have to play with some of that and look it all up. I'll get it eventually. Thanks

  7. #7
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I think I get it now. All I have to do is put my coloring code in a function (method), say it's called _setcolor. Then just call it's return result in the other function like this, $this->_setcolor() . Is that correct? Seems to work nicely.

  8. #8
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another question I have is what's the best way to connect to a db within a class. I have two methods that need to connect and pull data. Do I connect seperatly in each one (don't like that idea), or is there a better way (probably is)?

  9. #9
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Middle / East TN
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create a database connection class. I modeled mine after this:

    http://www.sitepointforums.com/showt...threadid=95763

    Then you just pass that object by reference to another class that needs it:

    $db =& new DatabaseConnection('username ....);
    $obj =& new Object ($db);

    Object would look like:
    PHP Code:
    class Object {
        var 
    $db;
        function 
    Object (& $db) {
            
    $this->db =& $db;
        }

    When your in a class, you can call a method (function) within the same class with $this->method(); It's when your accessing methods outside of the same class when you need to figure out the best way to get to it, read my post above.
    There are 10 kinds of people in this world,
    those who can read binary and those who can't.


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
  •