Jump to content

Recommended Posts

im having trouble understanding whey we need to create instance variables (data members) in a class

 

consider this code.

 

<?php
/* Filename: DataMember.php
* Created on: May 15, 2007
* Author: maxim
* Email:  ocaumaxim@gmail.com
*/

$instance = new DataMember();
$instance->printString();

class DataMember {

private $str;

public function __construct() {
	$this->str = 'Hello World!'; 
}

public function printString() {
	print $this->str;
}
}
?>

 

now if you comment out or delete the data member declaration

<? private $str; ?>

 

the script will still work fine. it has something to do with the $this variable. could someone please explain

Link to comment
https://forums.phpfreaks.com/topic/51473-instance-variables-aka-data-members/
Share on other sites

so basically there is absolutely no point what so ever in declaring a instance variable public. and i realize why they are meant to be private its so you cant access it directly form the instance, correct ?

iirc it raises an E_NOTICE error when you don't declare the variables.

 

If you want to create a value object or similar, you can declare the __set() and __get() magic methods to store data cleanly.

<?php
class ValueObject
{
    private $_data = array();

    public function __get ($name)
    {
        if (!isset($this->_data[$name])) throw new IndexOutOfBoundsException($name . ' is not a valid member');

        return $this->_data[$name];
    }

    public function __set ($name, $val)
    {
        $this->_data[$name] = $val;
    }
}

$foo = new ValueObject;

$foo->bar = 'Hello Worls!';

try
{
    echo $foo->bar;
}
catch (IndexOutOfBoundsException $e)
{
    echo '$foo->bar has not been set yet!';
}
?>

It's good practice to explicitly declare all of your member data, even though PHP will implicitly create new public member data for you if you assign a value.

 

I agree that it's a good practice, and improves the readability of your class. Declaring your variables gives you a standard place to comment on what that variable is for.

 

It also prevents the E_NOTICE errors which some programmers choose to ignore but each error comes at a processing expense. And displaying all errors while you are developing/debugging is a useful tool and can reduce time wasted when you mispell a variable somewhere along the way.

 

Also, if you use PHP5's overloading capabilities, you would NOT declare variables that you wish to overload. So declaring or not declaring even your public variables can therefore determine the behavior of your class.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.