Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP object oriented static properties.
Old 04-20-2010, 08:28 PM PHP object oriented static properties.
Junior Talker

Posts: 1
Trades: 0
Hi I am just learning the PHP object oriented stuff with a book I bought. The book is pretty good but it doesn't explain why the following code doesn't work:

PHP Code:
<?php

class StaticCall
{
    static 
$_static = -1;
    
    
    public function 
__construct()
    
    {
        
    
//static $_static = -1;
    
echo "<p>number is " $_static;
    
    
$_static 1;
    
    echo 
"</p><p> and number is " $_static;
    
    
    }
    
    
}

$static = new StaticCall();
The page is supposed to display the first value of $_static but its blank. like the property is undefined. The second value displays fine as 1. Can someone explain this to me? Or is it my server?

Last edited by chrishirst; 04-21-2010 at 10:08 AM..
cysten is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-20-2010, 08:43 PM Re: PHP object oriented static properties.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
When you reference a member variable of a class in PHP from within that class you need to use the $this reference:
PHP Code:
$this->_static
However, static variables and methods should always be referenced from the class definition rather than a particular instance. Using $this will generate this warning: Accessing static property StaticCall::$_static as non static.

When referencing a static variable do it like this:
PHP Code:
StaticCall::$_static 
Also, you shouldn't reassign static variables:
PHP Code:
$this->_static 1//don't do this 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-21-2010, 09:17 AM Re: PHP object oriented static properties.
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Also, within the class that the static member belongs to, you can refer to the variable with the "self" keyword:

PHP Code:
class AnyClass {
     static 
$_static 'awesome';
     function 
__construct() {
         echo 
self::$_static;//outputs 'awesome'
     
}

__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.

Last edited by wayfarer07; 04-21-2010 at 09:18 AM..
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to PHP object oriented static properties.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19012 seconds with 12 queries