Re: [PHP] public readonly variables

2010-04-23 Thread Richard Quadling
On 23 April 2010 10:55, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
 I know the subject line might sound a bit like an oxymoron, but bear
 with me!

 What I'd like to have is a property of an object that behaves like a
 public variable when it comes to reading, but like a protected one when
 it comes to writing, as it would make my code a lot easier to read.

 I know about the __get($var) method to magically provide this behaviour,
 but from all I've read it can be pretty slow compared to a regular
 public variable. I've seen a thread on the dev lists where someone
 requested it, but can't find anything in the manual.

 Does anyone know if this is available in a later version of PHP, or if
 it's implementation is penned for some time in the future?

 Thanks,
 Ash
 http://www.ashleysheridan.co.uk




What you are asking for is commonly known as accessibility. This has
been discussed on the list and is something I think would be a great
feature.

http://wiki.php.net/rfc/propertygetsetsyntax is an incomplete RFC (it
says it is incomplete at the top).

The last update was 2010/01/08 22:11, so a few months old.

Hopefully, some of the clever brains here can get to grips with it and
come to some consensus.


Whilst you can easily code __get()/__set() or getVar()/setVar()
methods to deal with it, with the later ones being significantly
easier to docblock, there is no easy way to document the property,
only the methods to set or get the property.


Richard.






-- 
-
Richard Quadling
Standing on the shoulders of some very clever giants!
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] public readonly variables

2010-04-23 Thread Adam Richardson
On Fri, Apr 23, 2010 at 6:19 AM, Richard Quadling
rquadl...@googlemail.comwrote:

 On 23 April 2010 10:55, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
  I know the subject line might sound a bit like an oxymoron, but bear
  with me!
 
  What I'd like to have is a property of an object that behaves like a
  public variable when it comes to reading, but like a protected one when
  it comes to writing, as it would make my code a lot easier to read.
 
  I know about the __get($var) method to magically provide this behaviour,
  but from all I've read it can be pretty slow compared to a regular
  public variable. I've seen a thread on the dev lists where someone
  requested it, but can't find anything in the manual.
 
  Does anyone know if this is available in a later version of PHP, or if
  it's implementation is penned for some time in the future?
 
  Thanks,
  Ash
  http://www.ashleysheridan.co.uk
 
 
 

 What you are asking for is commonly known as accessibility. This has
 been discussed on the list and is something I think would be a great
 feature.

 http://wiki.php.net/rfc/propertygetsetsyntax is an incomplete RFC (it
 says it is incomplete at the top).

 The last update was 2010/01/08 22:11, so a few months old.

 Hopefully, some of the clever brains here can get to grips with it and
 come to some consensus.


 Whilst you can easily code __get()/__set() or getVar()/setVar()
 methods to deal with it, with the later ones being significantly
 easier to docblock, there is no easy way to document the property,
 only the methods to set or get the property.


 Richard.






 --
 -
 Richard Quadling
 Standing on the shoulders of some very clever giants!
 EE : http://www.experts-exchange.com/M_248814.html
 EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
 Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
 ZOPA : http://uk.zopa.com/member/RQuadling

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


I've sometimes implemented a scheme where I create the variable, then unset
it in the constructor and rely on the magic method.  I know you were
concerned about the performance of magic methods, and I don't have a
solution for that :(

For example:

?php
/**
 * Description of PHPClass that documents nicely and still protects
variables from reassignment.
 *
 * @author Adam Richardson
 */
class PHPClass {
/**
 * Name for object.
 * @var String
 */
public $name;
/**
 * Size of something really important.
 * @var int
 */
public $size;
/**
 * Local storage of magic vars
 * @var array
 */
private $_vars = array();
/**
 * Create object and initialize instance vars.
 * @param array $instance_vars
 */
function __construct(array $instance_vars)
{
unset($this-name);
unset($this-size);

$this-_vars = $instance_vars;
}
function __get($name)
{
return $this-_vars[$name];
}
}

$obj = new PHPClass($instance_vars = array('name' = 'Billy', 'size' =
1000));

echo $obj-name;

?

Adam

-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com


Re: [PHP] public readonly variables

2010-04-23 Thread Ashley Sheridan
On Fri, 2010-04-23 at 11:57 -0400, Adam Richardson wrote:

 On Fri, Apr 23, 2010 at 6:19 AM, Richard Quadling
 rquadl...@googlemail.comwrote:
 
  On 23 April 2010 10:55, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
   I know the subject line might sound a bit like an oxymoron, but bear
   with me!
  
   What I'd like to have is a property of an object that behaves like a
   public variable when it comes to reading, but like a protected one when
   it comes to writing, as it would make my code a lot easier to read.
  
   I know about the __get($var) method to magically provide this behaviour,
   but from all I've read it can be pretty slow compared to a regular
   public variable. I've seen a thread on the dev lists where someone
   requested it, but can't find anything in the manual.
  
   Does anyone know if this is available in a later version of PHP, or if
   it's implementation is penned for some time in the future?
  
   Thanks,
   Ash
   http://www.ashleysheridan.co.uk
  
  
  
 
  What you are asking for is commonly known as accessibility. This has
  been discussed on the list and is something I think would be a great
  feature.
 
  http://wiki.php.net/rfc/propertygetsetsyntax is an incomplete RFC (it
  says it is incomplete at the top).
 
  The last update was 2010/01/08 22:11, so a few months old.
 
  Hopefully, some of the clever brains here can get to grips with it and
  come to some consensus.
 
 
  Whilst you can easily code __get()/__set() or getVar()/setVar()
  methods to deal with it, with the later ones being significantly
  easier to docblock, there is no easy way to document the property,
  only the methods to set or get the property.
 
 
  Richard.
 
 
 
 
 
 
  --
  -
  Richard Quadling
  Standing on the shoulders of some very clever giants!
  EE : http://www.experts-exchange.com/M_248814.html
  EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
  Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
  ZOPA : http://uk.zopa.com/member/RQuadling
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 I've sometimes implemented a scheme where I create the variable, then unset
 it in the constructor and rely on the magic method.  I know you were
 concerned about the performance of magic methods, and I don't have a
 solution for that :(
 
 For example:
 
 ?php
 /**
  * Description of PHPClass that documents nicely and still protects
 variables from reassignment.
  *
  * @author Adam Richardson
  */
 class PHPClass {
 /**
  * Name for object.
  * @var String
  */
 public $name;
 /**
  * Size of something really important.
  * @var int
  */
 public $size;
 /**
  * Local storage of magic vars
  * @var array
  */
 private $_vars = array();
 /**
  * Create object and initialize instance vars.
  * @param array $instance_vars
  */
 function __construct(array $instance_vars)
 {
 unset($this-name);
 unset($this-size);
 
 $this-_vars = $instance_vars;
 }
 function __get($name)
 {
 return $this-_vars[$name];
 }
 }
 
 $obj = new PHPClass($instance_vars = array('name' = 'Billy', 'size' =
 1000));
 
 echo $obj-name;
 
 ?
 
 Adam
 


I think for now I'll just resort to leaving it as a public variable.
I'll leave the specific set function for it in and just hope that is
used instead! As it's only me who'll be using it for the time being, I
can always yell at myself later if I forget!

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] public readonly variables

2010-04-23 Thread Peter Lind
On 23 April 2010 18:10, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
 I think for now I'll just resort to leaving it as a public variable.
 I'll leave the specific set function for it in and just hope that is
 used instead! As it's only me who'll be using it for the time being, I
 can always yell at myself later if I forget!

You're using a setter but a public variable? That's about the worst
compromise, isn't it? Either go down the road of the public variable
or the setter/getter (and in your case I would definitely recommend
the latter). Also, __get/__set are fine, as long as you don't use them
for everything (i.e. 5 magic calls per request will do very, very
little to your app, whereas 1000 per request will have some
significance on a site with lots of users).

Regards
Peter

-- 
hype
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
/hype

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] public readonly variables

2010-04-23 Thread Adam Richardson
On Fri, Apr 23, 2010 at 12:21 PM, Peter Lind peter.e.l...@gmail.com wrote:

 On 23 April 2010 18:10, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
  I think for now I'll just resort to leaving it as a public variable.
  I'll leave the specific set function for it in and just hope that is
  used instead! As it's only me who'll be using it for the time being, I
  can always yell at myself later if I forget!

 You're using a setter but a public variable? That's about the worst
 compromise, isn't it? Either go down the road of the public variable
 or the setter/getter (and in your case I would definitely recommend
 the latter). Also, __get/__set are fine, as long as you don't use them
 for everything (i.e. 5 magic calls per request will do very, very
 little to your app, whereas 1000 per request will have some
 significance on a site with lots of users).

 Regards
 Peter

 --
 hype
 WWW: http://plphp.dk / http://plind.dk
 LinkedIn: http://www.linkedin.com/in/plind
 Flickr: http://www.flickr.com/photos/fake51
 BeWelcome: Fake51
 Couchsurfing: Fake51
 /hype


I agree with Peter, that solutions asks for trouble (something I often do,
but avoid publicly advocating ;)

The solution I suggested still maintains all of the documentation
capabilities (at least in my NetBeans), but enforces protection.  It's not
perfect, but it does work relatively well.

Adam

-- 
Nephtali:  PHP web framework that functions beautifully
http://nephtaliproject.com


Re: [PHP] public readonly variables

2010-04-23 Thread Ashley Sheridan
On Fri, 2010-04-23 at 12:25 -0400, Adam Richardson wrote:

 On Fri, Apr 23, 2010 at 12:21 PM, Peter Lind peter.e.l...@gmail.com wrote:
 
  On 23 April 2010 18:10, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
   I think for now I'll just resort to leaving it as a public variable.
   I'll leave the specific set function for it in and just hope that is
   used instead! As it's only me who'll be using it for the time being, I
   can always yell at myself later if I forget!
 
  You're using a setter but a public variable? That's about the worst
  compromise, isn't it? Either go down the road of the public variable
  or the setter/getter (and in your case I would definitely recommend
  the latter). Also, __get/__set are fine, as long as you don't use them
  for everything (i.e. 5 magic calls per request will do very, very
  little to your app, whereas 1000 per request will have some
  significance on a site with lots of users).
 
  Regards
  Peter
 
  --
  hype
  WWW: http://plphp.dk / http://plind.dk
  LinkedIn: http://www.linkedin.com/in/plind
  Flickr: http://www.flickr.com/photos/fake51
  BeWelcome: Fake51
  Couchsurfing: Fake51
  /hype
 
 
 I agree with Peter, that solutions asks for trouble (something I often do,
 but avoid publicly advocating ;)
 
 The solution I suggested still maintains all of the documentation
 capabilities (at least in my NetBeans), but enforces protection.  It's not
 perfect, but it does work relatively well.
 
 Adam
 


I am probably looking at a lot of getters in the code though, so the
overhead I'd rather avoid. The setter is to go some way towards keeping
the values sane, which I realise goes against the whole public variable
thing, which is the reason for my original question.

Another reason for the setter is that it actually modifies a couple of
variables, so there's no good way of getting rid of that, as it would
then mean setting two properties of the object manually, which would
actually lead to more issues down the line if not set correctly.

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] public readonly variables

2010-04-23 Thread Peter Lind
On 23 April 2010 18:26, Ashley Sheridan a...@ashleysheridan.co.uk wrote:

 On Fri, 2010-04-23 at 12:25 -0400, Adam Richardson wrote:

 On Fri, Apr 23, 2010 at 12:21 PM, Peter Lind peter.e.l...@gmail.com wrote:

  On 23 April 2010 18:10, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
   I think for now I'll just resort to leaving it as a public variable.
   I'll leave the specific set function for it in and just hope that is
   used instead! As it's only me who'll be using it for the time being, I
   can always yell at myself later if I forget!
 
  You're using a setter but a public variable? That's about the worst
  compromise, isn't it? Either go down the road of the public variable
  or the setter/getter (and in your case I would definitely recommend
  the latter). Also, __get/__set are fine, as long as you don't use them
  for everything (i.e. 5 magic calls per request will do very, very
  little to your app, whereas 1000 per request will have some
  significance on a site with lots of users).
 
  Regards
  Peter
 
  --
  hype
  WWW: http://plphp.dk / http://plind.dk
  LinkedIn: http://www.linkedin.com/in/plind
  Flickr: http://www.flickr.com/photos/fake51
  BeWelcome: Fake51
  Couchsurfing: Fake51
  /hype
 

 I agree with Peter, that solutions asks for trouble (something I often do,
 but avoid publicly advocating ;)

 The solution I suggested still maintains all of the documentation
 capabilities (at least in my NetBeans), but enforces protection.  It's not
 perfect, but it does work relatively well.

 Adam


 I am probably looking at a lot of getters in the code though, so the overhead 
 I'd rather avoid. The setter is to go some way towards keeping the values 
 sane, which I realise goes against the whole public variable thing, which is 
 the reason for my original question.

 Another reason for the setter is that it actually modifies a couple of 
 variables, so there's no good way of getting rid of that, as it would then 
 mean setting two properties of the object manually, which would actually lead 
 to more issues down the line if not set correctly.


If you're just creating the project now, I'd autogenerate the classes,
to avoid the manual work. Otherwise, I'd give it some long thought
then grit my teeth and dig in.

Regards
Peter


--
hype
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
/hype

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] public readonly variables

2010-04-23 Thread Ashley Sheridan
On Fri, 2010-04-23 at 19:03 +0200, Peter Lind wrote:

 On 23 April 2010 18:26, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
 
  On Fri, 2010-04-23 at 12:25 -0400, Adam Richardson wrote:
 
  On Fri, Apr 23, 2010 at 12:21 PM, Peter Lind peter.e.l...@gmail.com wrote:
 
   On 23 April 2010 18:10, Ashley Sheridan a...@ashleysheridan.co.uk wrote:
I think for now I'll just resort to leaving it as a public variable.
I'll leave the specific set function for it in and just hope that is
used instead! As it's only me who'll be using it for the time being, I
can always yell at myself later if I forget!
  
   You're using a setter but a public variable? That's about the worst
   compromise, isn't it? Either go down the road of the public variable
   or the setter/getter (and in your case I would definitely recommend
   the latter). Also, __get/__set are fine, as long as you don't use them
   for everything (i.e. 5 magic calls per request will do very, very
   little to your app, whereas 1000 per request will have some
   significance on a site with lots of users).
  
   Regards
   Peter
  
   --
   hype
   WWW: http://plphp.dk / http://plind.dk
   LinkedIn: http://www.linkedin.com/in/plind
   Flickr: http://www.flickr.com/photos/fake51
   BeWelcome: Fake51
   Couchsurfing: Fake51
   /hype
  
 
  I agree with Peter, that solutions asks for trouble (something I often do,
  but avoid publicly advocating ;)
 
  The solution I suggested still maintains all of the documentation
  capabilities (at least in my NetBeans), but enforces protection.  It's not
  perfect, but it does work relatively well.
 
  Adam
 
 
  I am probably looking at a lot of getters in the code though, so the 
  overhead I'd rather avoid. The setter is to go some way towards keeping the 
  values sane, which I realise goes against the whole public variable thing, 
  which is the reason for my original question.
 
  Another reason for the setter is that it actually modifies a couple of 
  variables, so there's no good way of getting rid of that, as it would then 
  mean setting two properties of the object manually, which would actually 
  lead to more issues down the line if not set correctly.
 
 
 If you're just creating the project now, I'd autogenerate the classes,
 to avoid the manual work. Otherwise, I'd give it some long thought
 then grit my teeth and dig in.
 
 Regards
 Peter
 
 
 --
 hype
 WWW: http://plphp.dk / http://plind.dk
 LinkedIn: http://www.linkedin.com/in/plind
 Flickr: http://www.flickr.com/photos/fake51
 BeWelcome: Fake51
 Couchsurfing: Fake51
 /hype


I will be auto-generating the objects, but I'm not sure what you mean by
auto-generating the classes?

Thanks,
Ash
http://www.ashleysheridan.co.uk