Should You Access a Variable Within the Same Class via a Property

Should you access a variable within the same class via a Property?

One of the stronger argument for accessing local (class scope) variables through properties is that you add a level of abstraction in your class. If you change any logic concerning how that field is stored then the rest of your code will be left unaffected.

For example you might change that from a local variable to a property of a child object, to a database call, to a webservice call, to a static property on a class and so on. When making the change it gives you a single point of change, the property, and you do not have to update the rest of your class since they all use the property.

Also using the property enables you to apply business rules on the value of the property instead of having to enforce the same rule at each location where you'd directly access the field. Again, encapsulation

With the introduction of automatic properties there's even less reason to explicitly have a local variable, unless you need to apply business rules on the get/set

Should you reference the Property or the Member Variable inside a class?

This shouldn't be a choice you really make. Either the code in the setter is supposed to run, in which case use the property, or it's not, in which case you use the member variable. In most all situations one is right and one is wrong. Neither is always right/wrong in the general case, and it's unusual for it to "not matter".

For example, if the setter code is firing a "changed" event, do you want external objects to be notified that it changed, or not? If you're changing it in response to a previous change, probably not (infinite recursion anyone?) if no, you probably want to make sure it's fired (so that you're not changing a value and not notifying anyone of changes).

If it's just validating that the value being set is valid, then either you know that, in this context, the value is already validated and must be valid, in which case there is no need to validate again; set the property. If you haven't yet validated what you're about to set then you want the validation logic to run, so use the property.

How to access a class variable from same class

Either set the value in a constructor or create a getter to return the value.

// option 1

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product_variant extends Model
{

protected $primaryKey='variant_id';

public $translationForeignKey = '';

// option 1
public function __construct()
{
$this->translationForeignKey = $this->primaryKey;
}

}

// option 2, you dont even need the other property in this method, unless its value may change during execution

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product_variant extends Model
{

protected $primaryKey='variant_id';

// option 2
public function getTranslationForeignKey()
{
return $this->primaryKey;
}

}

Referencing variable in class from within the same class

You have declared methods static and your variable enabled as non static so you were not able to access it,

    public class CamShield
{
internal bool enabled = false;
public void start()
{
if(!enabled)
{
enabled = true;
//your code to start
}
}

public void stop()
{
if(enabled)
{
//your code to stop
enabled = false;
}
}
}

I am sure you can instantiate the CamShield class and access start and stop methods from outside.

How to access variables from other methods inside the same class in PHP?

If you are declaring it inside the method, you are out of luck unless you return the value.

class Profile {

public function index() {
$foo = 'bar';
return $foo;
}

public function form_submit() {
echo $this->index();
}
}

A perhaps better alternative would be to declare it as an object variable (what you describe as "at class level") but declare it private.

class Profile {

private $foo;

public function index() {
$this->foo = 'bar';
}

public function form_submit() {
echo $this->foo;
}

}

How would I access variables from one class to another?

var1 and var2 are instance variables. That means that you have to send the instance of ClassA to ClassB in order for ClassB to access it, i.e:

class ClassA(object):
def __init__(self):
self.var1 = 1
self.var2 = 2

def methodA(self):
self.var1 = self.var1 + self.var2
return self.var1

class ClassB(ClassA):
def __init__(self, class_a):
self.var1 = class_a.var1
self.var2 = class_a.var2

object1 = ClassA()
sum = object1.methodA()
object2 = ClassB(object1)
print sum

On the other hand - if you were to use class variables, you could access var1 and var2 without sending object1 as a parameter to ClassB.

class ClassA(object):
var1 = 0
var2 = 0
def __init__(self):
ClassA.var1 = 1
ClassA.var2 = 2

def methodA(self):
ClassA.var1 = ClassA.var1 + ClassA.var2
return ClassA.var1

class ClassB(ClassA):
def __init__(self):
print ClassA.var1
print ClassA.var2

object1 = ClassA()
sum = object1.methodA()
object2 = ClassB()
print sum

Note, however, that class variables are shared among all instances of its class.

Accessing member variables of other object of same class in Ruby

In ruby instance variables as well as private methods are accessible only to the object itself, not to any other object no matter their class. Protected methods are available to the object itself and other objects of the same class.

So to do what you want you can define a protected getter-method for your variable.

Edit: An example:

class Foo
protected
attr_accessor :my_variable # Allows other objects of same class
# to get and set the variable. If you
# only want to allow getting, change
# "accessor" to "reader"

public
def ==(other)
self.my_variable == other.my_variable
end
end


Related Topics



Leave a reply



Submit