This is a discussion on scope and public within the PHP Language forums, part of the PHP Programming Forums category; I'm working my way through learning php, it looks a lot like perl and then again it doesn't. ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I'm working my way through learning php, it looks a lot like perl and
then again it doesn't. I'm confused about scope of variables and public. The PHP manual sometimes leaves me confused. I have some very simple questions. #### I'm used to declaring global variables with either "our" or "my". It looks like in php, it's just. $my_variable='foo'; function my_function(){ $my_variable; // not defined; public $my_variable; // $my_variable now = 'foo'; is that right? } Is that the only way? Do I have that wrong? #### Now, I'm confused about the use of public in a class. class Foo{ $x='X'; public $y='Y'; } How is the public declaration different? Is it the same for instance, and unavailable as a class? #### I'm used to putting all my objects in separate file. What's the naming convention for doing this in PHP. Can I have methods of a class defined outside the class brackets? Can I do something like this: class Bar{ $some_method=someMethod(); } function someMethod(){ .... } #### Is this: $some_array=('one','two'); $some_array[]='three'; The same as push? Is there an easy way to add to the top of the array than the bottom? What about shifting an element of the array? And if you have mixed: my_array=('one','3'=>'three','2'=>'two'); How is that ordered? Jeff |
|
|||
|
..oO(Jeff)
> I'm working my way through learning php, it looks a lot like perl and >then again it doesn't. > >I'm confused about scope of variables and public. The PHP manual >sometimes leaves me confused. I have some very simple questions. > >#### > >I'm used to declaring global variables with either "our" or "my". > > It looks like in php, it's just. > >$my_variable='foo'; > >function my_function(){ > >$my_variable; // not defined; > >public $my_variable; > >// $my_variable now = 'foo'; is that right? >} > > Is that the only way? Do I have that wrong? It's just wrong syntax. Variables defined outside of classes or functions are global. If you want to make them visible inside of a function or method you have to use the 'global' keyword, in this example: $my_variable = 'foo'; function my_function() { global $my_variable; print $my_variable; // prints 'foo' } Without the 'global' statement the print would cause an E_NOTICE error. >#### > >Now, I'm confused about the use of public in a class. > >class Foo{ > >$x='X'; >public $y='Y'; >} > > How is the public declaration different? Is it the same for >instance, and unavailable as a class? Not sure what you mean with that. The above code will cause a parse error. The meaning of public, private and protected is explained in the manual. >#### > > I'm used to putting all my objects in separate file. What's the naming >convention for doing this in PHP. Whatever you like. My naming scheme: TFoo.class.inc IBar.interface.inc > Can I have methods of a class defined outside the class brackets? No. >Can >I do something like this: > >class Bar{ >$some_method=someMethod(); >} > >function someMethod(){ >... >} There are ways to "redirect" method calls to external functions, but the question is: Why? >#### > >Is this: > > $some_array=('one','two'); > >$some_array[]='three'; > > The same as push? Yes. The new value is pushed onto the end of the array. >Is there an easy way to add to the top of the array than the bottom? > > What about shifting an element of the array? Both can be done in various ways. See the array functions in the PHP manual. >And if you have mixed: > >my_array=('one','3'=>'three','2'=>'two'); > >How is that ordered? It's in the order it was created, unless you use one of the various sorting functions. Micha |
|
|||
|
Jeff wrote:
> I'm working my way through learning php, it looks a lot like perl and > then again it doesn't. > > I'm confused about scope of variables and public. The PHP manual > sometimes leaves me confused. I have some very simple questions. > -- SNIP -- > > #### > > Now, I'm confused about the use of public in a class. > > class Foo{ > > $x='X'; > public $y='Y'; > } > > How is the public declaration different? Is it the same for instance, > and unavailable as a class? > > #### class Foo { private $x = 'X'; public $y = 'Y'; var $z = 'Z'; public function test() { return $this-x; } } $fooInstance = new Foo(); print $fooInstance->y; The above example is for PHP5 class properties. The class property: - x is private to the class, so you cannot do $fooInstance->x, but can be accessed from within the class (see test method) - y is public class, so it can be accessed outside the class - z is PHP4 syntax, and in PHP 5 it is defaulted as PUBLIC variable Again, as Michael said, the manual explains this (see Object for php5 section) > -- SNIP -- > #### > > Is this: > > $some_array=('one','two'); > > $some_array[]='three'; > > The same as push? > > Is there an easy way to add to the top of the array than the bottom? > > What about shifting an element of the array? array_unshift() > -- SNIP -- > Jeff Classes in PHP5 is much more defined compared to PERL, as PERL classes are basically a "blessed" package/hash, where in PHP it's more like C++ You need to re-think how you are declaring classes. For example, you don't need to do this anymore for each method to get it's instance reference: $self = shift; Instead it's already defined by PHP automatically as $this Hope that helps Hendri Kurniawan |
|
|||
|
Ok, I just wanted to clarify between 2 thinks that you mentioned and I
don't feel were explained in a finite manner. PHP has scopes just like other languages, you can find every thing you want to know about that at http://us2.php.net/variables.scope The other thing you stumbled upon is called Access Modifiers. Access Modifiers are used in Object Oriented Programming(OOP) and are available in PHP as of version 5. Which a decent explanation of the different modifiers is found at http://www.hudzilla.org/phpbook/read.php/6_7_0 .. For sure if you have not done any OOP I would suggest reading up on the subject. Best Regards, Casey |
|
|||
|
caseyh wrote:
> Ok, I just wanted to clarify between 2 thinks that you mentioned and I > don't feel were explained in a finite manner. > > PHP has scopes just like other languages, you can find every thing you > want to know about that at http://us2.php.net/variables.scope > > The other thing you stumbled upon is called Access Modifiers. Access > Modifiers are used in Object Oriented Programming(OOP) and are > available in PHP as of version 5. Which a decent explanation of the > different modifiers is found at http://www.hudzilla.org/phpbook/read.php/6_7_0 This is great. The general lack of navigation forces you to go through each page, but since I need to learn the language this works. It's well written. I'll read it all. Jeff > . For sure if you have not done any OOP I would suggest reading up on > the subject. > > Best Regards, > > Casey > |
![]() |
| Thread Tools | |
| Display Modes | |
|
|