Re: scope and public
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
|