473,394 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

scope and public

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
Jun 27 '08 #1
3 1259
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
Jun 27 '08 #2
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

Jun 27 '08 #3
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
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hal Vaughan | last post by:
Being self taught, this is one thing I've always had trouble with -- I finally get it straight in one situation and I find I'm not sure about another. I have a class that keeps calling an...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
1
by: Chris Dunaway | last post by:
I use the class below as a base class to make object that execute in their own thread. If I create a derived class and instantiate an instance of that class, what happens to the thread if the...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
5
by: Rich | last post by:
The following code produced a singleton object with application scope when it should have had page scope: public class Singleton { private static Singleton uniqueInstance = null; private...
7
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
7
by: surfrat_ | last post by:
Hi, My project has about 4 source files (xxx.cs) and I am having a problem with scope between the files. If I put the code all within one class everything works OK. Can you please point me to...
3
by: Pantokrator | last post by:
Hi all, I've got a question about the scope of the constructor initialisation list. First of all, let me explain my classes: // ***************************************************** class...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
5
by: chromis | last post by:
Hi there, I've recently been updating a site to use locking on application level variables, and I am trying to use a commonly used method which copies the application struct into the request...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.