Questions tagged [class-constants]

In object-oriented programming, a class constant is a constant defined within a class.

In object-oriented programming, a class constant is a defined within a .

97 questions
5
votes
2 answers

Access constants in Class without making instance of class in ObjectiveC

Below C# class is used just to keep constants. So we can access each using class name So 'Constants.DIR_ARCHIVE' will give 'Archive'. How I can define below C# class in ObjectiveC? public class Constants { //Resource Directory Names public const…
4
votes
1 answer

Ruby Class method or Constant, best practice

One of my ruby classes draws data from a rather large, local, XML file that will only change with a new deploy. Is the best practice in this case to keep the document as a constant, like: class Product XML_DOC =…
SooDesuNe
  • 9,880
  • 10
  • 57
  • 91
4
votes
0 answers

Class constants in PHP strings: arrays yes, scalars not really

I was wondering if there was some way to interpolate class constants in strings without storing them in other variables first. Consider the following code: class Foo { const BAR = 'baz'; } $foo = new Foo(); // or $foo = 'Foo'; echo…
CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
4
votes
1 answer

Performance of MATLAB's class constants

I have several helper functions in my code which get called many times for a given numerical calculation. Those helper functions use some constant values for their calculations. The same constant value might be used by more than one helper…
FernAndr
  • 1,448
  • 1
  • 14
  • 26
4
votes
1 answer

Python class constant using class init method?

I've made a class which can be compared and sorted inside common data structures. The thing is that I wanted to make two class constants for the maximum and minimum values that class can take. So I could call this value just importing MyClass and…
madtyn
  • 1,469
  • 27
  • 55
4
votes
1 answer

Code style defining multiple constants in PHP

Is there any significant difference in how I define multiple constants in PHP class between: this:
DeadMoroz
  • 270
  • 1
  • 3
  • 13
4
votes
2 answers

create an array of all constant of a class?

I was working with a class where almost 20 constant are defined, as i want all these constant value in an array, i just want to know is there any method which create an array of all constant of a class? I tried with compact BUT it does not work with…
xkeshav
  • 53,360
  • 44
  • 177
  • 245
3
votes
5 answers

Using class constants and overriding in PHP

If I have a class structure with a value that can either be true or false, that doesn't change, currently implemented as variables would it be better to change them to constants, such as: class Parent { const BOOL_CONST = false; …
AntonChanning
  • 509
  • 2
  • 13
  • 37
3
votes
1 answer

What is the difference between a constant member and a private member without setter?

Suppose I have a constant member and different object have different values for this constant, what is the difference between this constant member and a private member without a setter?
samnaction
  • 1,194
  • 1
  • 17
  • 45
3
votes
1 answer

PHP: Constants & Bitwise separator as default argument to a method

Im creating a class method and want to have a default argument value that contains constants: MQSERIES_MQGMO_FAIL_IF_QUIESCING | MQSERIES_MQGMO_WAIT, 'WaitInterval'…
Paul Wieland
  • 765
  • 2
  • 10
  • 29
3
votes
4 answers

define constant in php

how do I define a constant inside a function eg. class { public test; function tester{ const test = "abc"; } }
Jeremy Gwa
  • 2,333
  • 7
  • 25
  • 31
3
votes
3 answers

Ruby "CONSTANTS" seem to be INVISIBLY ALTERABLE?

I understand that "constants" in Ruby are by convention called constants but are in fact mutable. However I was under the impression that when they were "mutated" that there was a warning: class Z2 M = [0,1] end Z2::M # => [0, 1] Z2::M =…
TJChambers
  • 1,489
  • 1
  • 18
  • 28
3
votes
2 answers

How to implement a lookup class in Ruby?

I have a list of immutable value objects. The lookup class provides ways to iterate and query that data: class Banker Bank = Struct.new(:name, :bic, :codes) attr_reader :banks def initialize @banks = [ Bank.new('Citibank', …
randomguy
  • 12,042
  • 16
  • 71
  • 101
3
votes
2 answers

PHP Class constant with php function constant giving warning

I have a constants I want to return by a function like this: public function getConst($const) { $const = constant("Client::{$const}"); return $const; } But this is giving me an error: constant(): Couldn't find constant…
Dion Snoeijen
  • 94
  • 2
  • 11
3
votes
2 answers

PHP Object access with class constant

Is it possible in PHP to access a member of an object where the name of the member is specified by a class constant? Consider this example: class X{ const foo = "abc"; } class Y{ public $abc; } $y = new Y(); $y->X::foo = 23; //This does…
gexicide
  • 38,535
  • 21
  • 92
  • 152