This is a discussion on PHP5 object construct within the PHP General forums, part of the PHP Programming Forums category; I read about a feature of PHP5 OOP that is something like this in a book or a magazine a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I read about a feature of PHP5 OOP that is something like this in a book or
a magazine a while back. But now I don't remember exactly how this works and I can't find any reference to it in the online docs. The basic idea is something along these lines: class MyClass { private $bob; public $Bob { set( $var ) { $this->bob = $var; } get() { return $this->bob; } } } Could someone point me to the relevant section of the docs that discusses this? thnx, Chris |
|
|||
|
http://us2.php.net/class
On 9/22/06, Chris Boget <chris.boget@wild.net> wrote: > I read about a feature of PHP5 OOP that is something like this in a book or > a magazine a while back. But now I don't remember exactly how this works > and I can't find any reference to it in the online docs. The basic idea is > something along these lines: > > class MyClass { > > private $bob; > public $Bob { > set( $var ) { > $this->bob = $var; > > } > get() { > return $this->bob; > > } > } > } > > Could someone point me to the relevant section of the docs that discusses > this? > > thnx, > Chris > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > |
|
|||
|
> well, perhaps I'm not seeing what it is that you're looking for.
No, I'm not sure you are. Take a look at my sample code again. Pay particular attention to how both $bob and $Bob are defined. class MyClass { private $bob; public $Bob { set( $var ) { $this->bob = $var; } get() { return $this->bob; } } } thnx, Chris |
|
|||
|
PHP has __set and __get in some versions... I think that's what you are looking for. If you told us which particular feature in that pile of code you're asking about, it would help... I still wouldn't know the answer, as I don't do OOP in PHP much, but somebody might. On Fri, September 22, 2006 7:32 pm, Chris Boget wrote: >> well, perhaps I'm not seeing what it is that you're looking for. > > No, I'm not sure you are. Take a look at my sample code again. Pay > particular attention to how both $bob and $Bob are defined. > > class MyClass { > > private $bob; > > public $Bob { > set( $var ) { > $this->bob = $var; > > } > get() { > return $this->bob; > > } > } > } > > thnx, > Chris > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Like Music? http://l-i-e.com/artists.htm |
|
|||
|
> ok, so if we were talking Java, perhaps you are looking for
> information that allows you to build 'accessor' and 'mutator' methods? Yes, exactly. I was flipping through a PHP5 book (possibly a magazine, but it was definitely about PHP5 and not Java) and I saw this new language construct. Since I've done a bit of dabbling in C#, have used it there and found it useful, I thought it was really cool that PHP5 had implemented it. thnx, Chris |
|
|||
|
> ok, so if we were talking Java, perhaps you are looking for
> information that allows you to build 'accessor' and 'mutator' methods? > If so, then your example should work (syntax aside). Here's another > 'test' example that I just whipped up and tested that shows you can > use any method name you wish. As I said previously, accessor/mutator methods are exactly what I am looking for. I do like the idea of having a public 'interface' (for the lack of a better word) to a private property. I don't particularly care for using just the __set() and __get() methods because they are too arbitrary. If I needed to do any specific checking or formatting for a property, either in defining or returning the value, I'd have to have a big giant switch() statement. So here is a roundabout solution that I got the idea/code for while researching this issue: class MyClass { private $_bob; // Private so all property access is done through the // __set() and __get() methods private function setBob( $bob ) { $this->_bob = $bob; } private function getBob() { return $this->_bob; } function __get( $var ) { $methodName = 'get' . $var; if( method_exists( $this, $methodName )) { return call_user_func( array( $this, $methodName )); } else { throw new Exception( 'Method [' . $methodName . '] does not exist' ); } // echo "In __get( $var )\n"; } function __set( $key, $var ) { $methodName = 'set' . $key; if( method_exists( $this, $methodName )) { call_user_func( array( $this, $methodName ), $var ); } else { throw new Exception( 'Method [' . $methodName . '] does not exist' ); } // echo "In __set( $key, $var )\n"; } function __call( $method, $args ) { // echo 'Calling [' . $method . '] with args: [' . print_r( $args, TRUE ) .. ']'; } } try { $myClass = new MyClass(); $myClass->Bob = 'JoeBobBriggs' ; echo '$myClass: <pre>' . print_r( $myClass, TRUE ) . '</pre>'; echo $myClass->Bob . ''; } catch( Exception $e ) { echo 'Caught exception: ', $e->getMessage(), "\n"; } Now I can do whatever checking I need to do in the setBob() method and any formatting in the getBob() method. The above is not my original idea and I wish I could remember the name of the person to thank for pointing me in this direction. :| thnx, Chris |