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 04-20-2008, 06:25 PM   #1 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 12
Thanks: 7
Durux is on a distinguished road
Default How do i lock a variable in a class

How do i lock a variable in a class so that i can read it but not change it in an instance, without using return.

Class EX:
PHP Code:
class MyClass
{
    public 
$var;
    
    public function 
ChangeVar(){
        
$this->var rand(1,20);
    }

With this i can still change the variable inside the class though.

PHP Code:
$class = new MyClass;

$class->ChangeVar();
//But i can also do this, though i don't want to
$class->var 50
Is it at all possible to do without using return?

EDIT:
Using PHP5 btw.
Durux is offline  
Reply With Quote
Old 04-20-2008, 06:43 PM   #2 (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

Set the visibility to either private or protected. Then to allow reading the value you can use a public method (function) or hook into the __get method.

PHP Code:
class MyClass
{
    protected 
$var;
    
    
// ...

    
public function getVar()
    {
        return 
$this->var;
    }
    
// or
    
public function __get($property)
    {
        if (
$property === 'var')
            return 
$this->var;
    }

Salathe is offline  
Reply With Quote
Old 04-20-2008, 08:39 PM   #3 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 12
Thanks: 7
Durux is on a distinguished road
Default

Yes i now that, but the question was if it was possible without using the return command.
Durux is offline  
Reply With Quote
Old 04-20-2008, 08:45 PM   #4 (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

Possible, yes. A viable alternative, no.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Durux (04-20-2008)
Old 04-20-2008, 09:04 PM   #5 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

the return is used for reading, you asked about changing it's value?
Well, you can't 'lock' a variable to read only inside a class.
It is supposed that a class is a totally separate entity and you are the one who read/write class's properties unless you don't trust yourself :)
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 04-20-2008, 09:11 PM   #6 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 12
Thanks: 7
Durux is on a distinguished road
Default

Quote:
Originally Posted by freenity View Post
the return is used for reading, you asked about changing it's value?
Well, you can't 'lock' a variable to read only inside a class.
It is supposed that a class is a totally separate entity and you are the one who read/write class's properties unless you don't trust yourself :)
Well i do trust myself, but i just see it as a better solution to be able to 'lock' a variable so only the class and not the instance can give the variable a value.
It would decrease the use of return in a class and you would not need to assign a new variable outside the class to hold the value.
Durux 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 04:53 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