PHP5 object construct

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-23-2006
Chris Boget
 
Posts: n/a
Default PHP5 object construct

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
Reply With Quote
  #2 (permalink)  
Old 09-23-2006
Joe Wollard
 
Posts: n/a
Default Re: [PHP] PHP5 object construct

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
>
>

Reply With Quote
  #3 (permalink)  
Old 09-23-2006
Chris Boget
 
Posts: n/a
Default Re: Re: [PHP] PHP5 object construct

> 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
Reply With Quote
  #4 (permalink)  
Old 09-23-2006
Richard Lynch
 
Posts: n/a
Default Re: Re: [PHP] PHP5 object construct



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
Reply With Quote
  #5 (permalink)  
Old 09-23-2006
Chris Boget
 
Posts: n/a
Default Re: [PHP] PHP5 object construct

> 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
Reply With Quote
  #6 (permalink)  
Old 09-23-2006
Chris Boget
 
Posts: n/a
Default Re: [PHP] PHP5 object construct

> 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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:55 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0