TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-07-2009, 02:16 PM   #21 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Its been a long time since I've worked with PHP, I never got too in depth to whichever these are (in PHP). So you may very well be right.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-07-2009, 03:12 PM   #22 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Sketch is along the right lines. References in PHP are not pointers at all, they're just aliased variable names in a symbol table. In PHP, values are kept in a zval container which keeps track of several things about them; not just the value of the variable but its type and some other things.

Variables are stored in symbol tables mapping variable names to their associated values. When talking about references, we're not talking about one variable referencing another.

Instead, multiple variable names being associated with the same zval container (and therefore the same value) which has its is_ref flag set to 1. To perhaps confuse things, multiple variable names can be associated with the same zval container and not be references (is_ref set to 0), merely sharing that same value.
Salathe is offline  
Reply With Quote
Old 05-07-2009, 03:55 PM   #23 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
Sketch is along the right lines. References in PHP are not pointers at all, they're just aliased variable names in a symbol table. In PHP, values are kept in a zval container which keeps track of several things about them; not just the value of the variable but its type and some other things.

Variables are stored in symbol tables mapping variable names to their associated values. When talking about references, we're not talking about one variable referencing another.

Instead, multiple variable names being associated with the same zval container (and therefore the same value) which has its is_ref flag set to 1. To perhaps confuse things, multiple variable names can be associated with the same zval container and not be references (is_ref set to 0), merely sharing that same value.
Even after a cup of coffee, it still doesn't help....

I assume that what you say here is referring to pass-by-ref?

Quote:
multiple variable names being associated with the same zval container (and therefore the same value) which has its is_ref flag set to 1

What is this technique below? How is it done? And is there a name for this?

Quote:
To perhaps confuse things, multiple variable names can be associated with the same zval container and not be references (is_ref set to 0), merely sharing that same value.
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 03:55 PM   #24 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Oh. For those who now understand pointers, you have grasped the hardest beginner concept for both C and C++.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-07-2009, 03:57 PM   #25 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
I was under the impression that PHP's references functioned more like an alias (like a reference in C++) rather than a pointer, in other words they don't point to a memory location but are an alternative name for the target (think of it as a symlink), they more or less achieve the same but are different in terms of what you can do with them, you cant re-seat a reference as you can a pointer (in C++ anyway), you can't have a null reference whereas you can have a null pointer also you can't perform pointer arithmetic on references (which some would consider to be a good thing).

In terms of PHP however, you won't really need an in-depth knowledge of the differences between the too, they will both provide more or less the same functionality (in any case you can't directly fiddle with the memory in PHP like you can in C as its interpreted not compiled).

Just for the sake of illustration, heres a C++ pointer:
c++ Code:
#include <iostream>
using namespace std;

int main()
{
    unsigned short int age = 21;
    unsigned short int * pAge = 0;
    pAge =& age;

    cout << "Age: " << age << "pointer age: " << *pAge << endl;

    *pAge = 25;

    cout << "Age: " << age << "pointer age: " << *pAge << endl;
   
    age = 22;

    cout << "Age: " << age << "pointer age: " << *pAge << endl;

    return 0;
}

and a C++ reference:
c++ Code:
#include <iostream>
using namespace std;

int main()
{
    unsigned short int age = 21;
    unsigned short int&  rAge = age;
   
    cout << "Age: " << age << "ref age: " << rAge << endl;
   
    age = 25;

    cout << "Age: " << age << "ref age: " << rAge << endl;

    rAge = 22;

    cout << "Age: " << age << "ref age: " << rAge << endl;
   
    return 0;
}

Heartburn!!!!


I don't have a compiler avaiable, can someone provide the output for these 2 code snippets?
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 04:06 PM   #26 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Here's a pretty picture to tell the tale. The diagrams show two similar sets of code, one using "normal" assignments by value and the other using references. The single letters are variable names, the detail boxes represent the zval container (a C struct) which contains the value, the type, an is_ref flag and the count of "references" (in the latter case, the count of variable names associated with this container). The green highlights are what changes.


P.S. The term "pass" is generally reserved for feeding arguments into function calls. Stick with "assign" for assignment operations.
Salathe is offline  
Reply With Quote
Old 05-07-2009, 04:07 PM   #27 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
you can't have a null reference whereas you can have a null pointer also you can't perform pointer arithmetic on references
Ok, I wanted to test this, how come I didn't get any error?

PHP Code:
<?php


$var100 
NULL;

$var200 =& $var100;

$get_type gettype($var100);
$get_type2 gettype($var200);

echo 
"Type: " $get_type "<br>";
echo 
"Type2: " $get_type2 "<br>";

?>
Output is:

Type: NULL
Type2: NULL


Shouldn't there be an error for Type2? Like, "Error, reference cannot be null"
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 05:40 PM   #28 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
Here's a pretty picture to tell the tale. The diagrams show two similar sets of code, one using "normal" assignments by value and the other using references. The single letters are variable names, the detail boxes represent the zval container (a C struct) which contains the value, the type, an is_ref flag and the count of "references" (in the latter case, the count of variable names associated with this container). The green highlights are what changes.


P.S. The term "pass" is generally reserved for feeding arguments into function calls. Stick with "assign" for assignment operations.
Lovely! Ok, some quick questions...

is_ref is a flag? It only takes 0 or 1 right?

0 = no pass-by-ref..so value is pass-by-value?
1 = yes, someone is using pass-by-ref on this value

refcount = just some kind of simple counter of who is "accessing" this data. Doesn't matter if it's by ref or by value....


So like I've mentioned before, it seems that by-ref is more than just a looking glass to the data, but actually provides a medium to modify that data at that stored location.

And like the Borg, any other variables accessing this value also receives the new changes...


My question is, "Is PHP not using the correct terminology?"

Are they confusing individuals like myself by using pass-by-ref when it should be assign-by-ref?
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 06:20 PM   #29 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
Oh. For those who now understand pointers, you have grasped the hardest beginner concept for both C and C++.
Indeed! took me a few attempts to crack this egg myself.

Quote:
I don't have a compiler avaiable, can someone provide the output for these 2 code snippets?
oops, sorry i forgot.

1st:
Code:
Age: 21 pointer age: 21

Age: 25 pointer age: 25

Age: 22 pointer age: 22
2nd:
Code:
Age: 21 ref age: 21

Age: 25 ref age: 25

Age: 22 ref age: 22
I'll explain the null reference (which in C++ can cause some arguments) when i get home, unless ofc someone else has stepped into the fray!
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-07-2009, 07:30 PM   #30 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
is_ref is a flag? It only takes 0 or 1 right?

0 = no pass-by-ref..so value is pass-by-value?
1 = yes, someone is using pass-by-ref on this value

refcount = just some kind of simple counter of who is "accessing" this data. Doesn't matter if it's by ref or by value....
Yes, that's right.

Quote:
Originally Posted by allworknoplay View Post
My question is, "Is PHP not using the correct terminology?"

Are they confusing individuals like myself by using pass-by-ref when it should be assign-by-ref?
I'm not really sure where you are getting this from, perhaps there are places in the documentation which might be unclear (or wrong), the docs are written (mostly) by humans after all. Where have you been reading the confusing terms? The difference between passing by reference and assigning by reference is the same as passing and assigning by value.

PHP Code:
function byref(&$foo){}
function 
byval($foo){}

$byref =& $blah// Assign by reference
$byval =  $blah// Assign by value
byref($blah);    // Pass by reference
byval($blah);    // Pass by value

// Also
byval(&$blah); // Call-time pass by reference, deprecated 
Salathe is offline  
Reply With Quote
Old 05-07-2009, 08:04 PM   #31 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
Yes, that's right.


The difference between passing by reference and assigning by reference is the same as passing and assigning by value.
That's where I was confused. I thought they were 2 different beasts when I was reading Sketch's post about how PHP isn't really using a pointer but more of an alias(like symbolic links)..

So I thought what he was trying to imply was pass-by-ref had a different fundamental meaning than assign-by-ref.

But thanks for clearing that up, I get it now...
allworknoplay is offline  
Reply With Quote
Old 05-07-2009, 11:54 PM   #32 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Once again another great and informative post from Salathe

By null reference I am referring to a memory reference that is null (either null or 0), in your example the memory location (or whatever PHP does) will be a non-null one as your variable is defined (thus memory set aside with a correct memory location thats non null) then a value of null is assigned (does it get cast to 0 internally?).

For a null reference (in C++ anyway, i know i said you can't but i had my strict standards hat on at the time, i should say you can but you shouldnt, i believe its allowed for certain embedded hardware stuff where you need a specific memory location for things) you dereference a null pointer (meaning it has a memory location of null/0) and try to reference it, thus this will die:
c++ Code:
#include <iostream>
using namespace std;
typedef unsigned short int USHORT;

int main()
{
    USHORT * p = 0;
    USHORT & ref = *p;
    cout << ref;
    return 0;
}

This seg faults (or crashes depending on the machne and OS) as it tries to access a null memory location (when cout is called), which doesn't exist.

for null in C++ you can either include stddef.h which has a macro defined for null, or you could cast it:
c++ Code:
USHORT * p = (USHORT *) NULL;

I dunno if you can replicate it in PHP.

Sorry for any confusion, guess I got carried away with C++, it happens sometimes.

If you have any more questions, I will try to answer them ( promise I'll try NOT to throw a load of C++ junk at you , I just thought I'd explain pointers and references, as VI said, the one of the hardest concepts to understand)

Hope that helps.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-08-2009, 02:00 AM   #33 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
Once again another great and informative post from Salathe

By null reference I am referring to a memory reference that is null (either null or 0), in your example the memory location (or whatever PHP does) will be a non-null one as your variable is defined (thus memory set aside with a correct memory location thats non null) then a value of null is assigned (does it get cast to 0 internally?).

For a null reference (in C++ anyway, i know i said you can't but i had my strict standards hat on at the time, i should say you can but you shouldnt, i believe its allowed for certain embedded hardware stuff where you need a specific memory location for things) you dereference a null pointer (meaning it has a memory location of null/0) and try to reference it, thus this will die:
c++ Code:
#include <iostream>
using namespace std;
typedef unsigned short int USHORT;

int main()
{
    USHORT * p = 0;
    USHORT & ref = *p;
    cout << ref;
    return 0;
}

This seg faults (or crashes depending on the machne and OS) as it tries to access a null memory location (when cout is called), which doesn't exist.

for null in C++ you can either include stddef.h which has a macro defined for null, or you could cast it:
c++ Code:
USHORT * p = (USHORT *) NULL;

I dunno if you can replicate it in PHP.

Sorry for any confusion, guess I got carried away with C++, it happens sometimes.

If you have any more questions, I will try to answer them ( promise I'll try NOT to throw a load of C++ junk at you , I just thought I'd explain pointers and references, as VI said, the one of the hardest concepts to understand)

Hope that helps.

No need to apologize. You're just bringing back memories from my C++ days in college...

I really appreciate the time all of you guys are taking to explain the basics of PHP. Just when you think you know something, you find out there's more to it...

No matter where I find myself in PHP land, I am never afraid to go back to basics and making sure I get the fundamentals right.

It all clicks now for me, I hope I don't get blind sided by anything else pass-by-ref or pass-by-val related!!!
allworknoplay is offline  
Reply With Quote
Old 05-08-2009, 10:41 AM   #34 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
You're just bringing back memories from my C++ days in college...
Yea I had to dig deep myself, its along time since my last C++ program. Hopefully i got my facts right.

Quote:
I really appreciate the time all of you guys are taking to explain the basics of PHP. Just when you think you know something, you find out there's more to it...
No probs m8, In fact it helps me also as it acts as a refresher of stuff I have known at some point but have forgotten (Got to the stage where new stuff forces the old stuff out of my memory)

Glad you found it useful
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pulling values from one site as reference 9three Absolute Beginners 4 02-17-2009 02:37 PM
Desktop icon to pass login? oMIKEo Absolute Beginners 7 02-22-2008 09:55 PM
Call Time by pass by Reference Deprecated??? :S Orc General 8 01-28-2008 09:20 PM
Make Your Own PHP Flash Player Which You Can Pass a File Variable with Preview!!!! thegrayman Tips & Tricks 2 12-12-2007 12:44 AM
Lowdown on Passing Things as Reference Wildhoney General 0 09-28-2007 10:57 AM


All times are GMT. The time now is 06:47 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design