Click here to get back home

scope and public

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
scope and public Jeff 06-12-2008
|--> Re: scope and public Hendri Kurniawa...06-13-2008
Posted by Jeff on June 12, 2008, 1:40 pm
Please log in for more thread options
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



Posted by Hendri Kurniawan on June 13, 2008, 9:52 pm
Please log in for more thread options
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

Posted by caseyh on June 14, 2008, 5:01 am
Please log in for more thread options
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


Posted by Jeff on June 14, 2008, 6:56 pm
Please log in for more thread options
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
>

Similar ThreadsPosted
.php files - permissions are set public readable - so can public get to see actual raw file? January 26, 2005, 1:13 am
Getting public key using openssl_* August 24, 2005, 11:20 pm
variable scope August 14, 2005, 9:29 pm
PHP static scope January 23, 2006, 2:23 pm
can i get the public key of client machine using php August 1, 2004, 1:37 am
Class public function December 7, 2005, 9:51 am
Public response to A Kaufman February 1, 2007, 6:14 am
Writing to files that are not public March 8, 2007, 8:03 am
A public appology to ZeldorBlat May 10, 2007, 12:02 pm
Securing public images June 18, 2007, 2:22 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap