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 02-14-2008, 07:22 PM   #1 (permalink)
The Wanderer
 
cherries's Avatar
 
Join Date: Oct 2007
Posts: 20
Thanks: 0
cherries is an unknown quantity at this point
Default static variables and functions in classes?

What's the use of the static keyword for classes?
PHP Code:
<?php
    
class Something
    
{
        private static 
$variable '0';
        
        private static function 
Test $variables )
        {
            
// ??????
        
}
    }
?>
Also I've been wondering what =& is for..
cherries is offline  
Reply With Quote
Old 02-14-2008, 07:56 PM   #2 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

I think you'll want to take a look at this page; PHP: Static Keyword - Manual

And of course the complete introduction to OOP PHP 5 is available to your as well.
PHP: Classes and Objects (PHP 5) - Manual

I have never heard about or seen the operator (assumed operator) =&, but here is a list of operators commonly used in PHP 4 and 5.

PHP Operators

Good luck.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-14-2008, 08:07 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

=& is merely for passing something by reference.

php Code:
$a = 5;
$b = 6;
$b =& $a; // This is where the reference is made
$a = 2;
var_dump($b); // Now equals 2 because it's a reference to "a"
 
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
ReSpawN (02-14-2008)
Old 02-14-2008, 08:48 PM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Thanks Wildhoney, I didn't know that. So if I understand it correctly;
PHP Code:
$a 5// Define "a" as 5
$b 6// Define "b" as 6
$b =& $a// $b is now equal to the FIRST "a"
$a 2// "a" is redefined as 2
var_dump($b); // Since "a" was 6 and is now "2", $b is referenced to the latest 'version' of "a" 
Correct? If so, thanks again.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-14-2008, 09:29 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
Thanks Wildhoney, I didn't know that. So if I understand it correctly;
PHP Code:
$a 5// Define "a" as 5
$b 6// Define "b" as 6
$b =& $a// $b is now equal to the FIRST "a"
$a 2// "a" is redefined as 2
var_dump($b); // Since "a" was 6 and is now "2", $b is referenced to the latest 'version' of "a" 
Correct? If so, thanks again.
Yeap, that's correct
__________________
Tanax is offline  
Reply With Quote
Old 02-14-2008, 09:31 PM   #6 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Thanks Tanax! :)
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-15-2008, 02:08 PM   #7 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

When declaring a property or method static they should be called outside a the class context ($this or a class instance made outside) with a double colon:

PHP Code:
ClassName::StaticMethod();

echo 
ClassName::$property;


/** Namespace */
echo NamespaceName::ClassName::$property
and so on ;)

Last edited by Kalle : 02-18-2008 at 02:12 PM.
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 02-15-2008, 02:22 PM   #8 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

I as well am kinda new to the OOP in PHP 5, so what does ClassName::$property do? (the dots honey)
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-15-2008, 03:29 PM   #9 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

ClassName::$property just returns the value of $property
sjaq is offline  
Reply With Quote
Old 02-15-2008, 07:40 PM   #10 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Just to clear something up. The ampersand operator (not the equal-ampersand operator) is used to return the memory handle of a variable/object/what ever. Therefore, all the following assignments are valid:

Code:
$a =& $x;
$a = &$x;
$a = & $x;
// and so on...
And to make it more clear, $a and $x are not equal. They just point to the same memory address. $x contains a value, while $a contains only the memory address in which the value of $x is stored.

Sounds confusing, but it's not.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 02-18-2008, 01:46 AM   #11 (permalink)
The Wanderer
 
cherries's Avatar
 
Join Date: Oct 2007
Posts: 20
Thanks: 0
cherries is an unknown quantity at this point
Default

Thanks a lot :)
cherries is offline  
Reply With Quote
Old 02-18-2008, 02:16 PM   #12 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
I as well am kinda new to the OOP in PHP 5, so what does ClassName::$property do? (the dots honey)
Like sjaq said, It returns the property value and its valid in PHP4+. The reason not to use just 'ClassName::Something' is that from PHP5 they are constants.

The double colon or 'Paamayim Nekudotayim' (Double colon in hebrew, PHP only) is simply the way of calling static methods, theres some information about it on the PHP manual here:
PHP: Scope Resolution Operator (::) - Manual
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle 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


All times are GMT. The time now is 07:30 PM.

 
     

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