Asked  7 Months ago    Answers:  5   Viewed   33 times

I have several CONST's defined on some classes, and want to get a list of them. For example:

class Profile {
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";
}

Is there any way to get a list of the CONST's defined on the Profile class? As far as I can tell, the closest option(get_defined_constants()) won't do the trick.

What I actually need is a list of the constant names - something like this:

array('LABEL_FIRST_NAME',
    'LABEL_LAST_NAME',
    'LABEL_COMPANY_NAME')

Or:

array('Profile::LABEL_FIRST_NAME', 
    'Profile::LABEL_LAST_NAME',
    'Profile::LABEL_COMPANY_NAME')

Or even:

array('Profile::LABEL_FIRST_NAME'=>'First Name', 
    'Profile::LABEL_LAST_NAME'=>'Last Name',
    'Profile::LABEL_COMPANY_NAME'=>'Company')

 Answers

69

You can use Reflection for this. Note that if you are doing this a lot you may want to looking at caching the result.

<?php
class Profile {
    const LABEL_FIRST_NAME = "First Name";
    const LABEL_LAST_NAME = "Last Name";
    const LABEL_COMPANY_NAME = "Company";
}


$refl = new ReflectionClass('Profile');
print_r($refl->getConstants());

Output:

Array
(
    'LABEL_FIRST_NAME' => 'First Name',
    'LABEL_LAST_NAME' => 'Last Name',
    'LABEL_COMPANY_NAME' => 'Company'
)
Wednesday, March 31, 2021
 
laurent
answered 7 Months ago
68
exec("wmic /node:$_SERVER[REMOTE_ADDR] COMPUTERSYSTEM Get UserName", $user);
echo($user[1]);

(This wouldn't work if your PHP server is hosted on a non-windows machine or your linux host has wmic capabilities extended via other tools)

Saturday, May 29, 2021
 
madphp
answered 5 Months ago
86

Clearify the question: What is the advantage of a configuration container over globally define()ed configuration contstants?

The advantages are all the advantages that OOP offers: Data Abstraction and Encapsulation, Inheritance, Polymorphism and better design pattern integration.

The readers of this thread seem a little confused and focus on your class rather than the main question. To also clearify this, let me give you an example:

class Configuration
{
    protected $someValue;
}

class ConfigurationDev extends Configuration
{
    protected $baseUrl = 'http://devel.yoursite.com/';
}

class ConfigurationLive extends Configuration
{
    protected $baseUrl = 'http://www.yoursite.com/';
}

index.php:

<?php
$config   = new ConfigurationDev;
$tracking = new Tracking($config);
...

class Tracking:

class Tracking
{
    public function __construct(Configuration $config) {
        if ($config instanceof ConfigurationLive) {
            // We are in live environment, do track
        } else {
            // Debug Notice: We are NOT in live environment, do NOT track
        }
    }
}

Explanation of the scenario:

Imagine you want to track users, but only on the live system, not on your development system. The Tracking class expects a live configuration but aborts (without impact) if its not the live config.

Your class with const is not the best, because const implies you do not want to change the variables. Do not use the variable for values that may change. You shouldn't use static stuff either because it mostly conflicts with dependency injection. Pass real objects!

Your function public static function getValueDependingOnURL() should be placed in a Helper class, not in a Constant/Configuration container either.

class Helper
{
    protected $config;

    public function __construct(Configuration $config) {
        $this->config = $config;
        return $this;
    }

    public function getValueByUrl($url) {
        if ($url == 'something') {
            return $config->getMinValue();
        } else {
            return $config->getMaxValue();
        }
    }
}

Now you can have different sets of configuration which the helper class relies on:

$config = new ConfigurationLive;
$helper = new Helper($config);
$value  = $helper->getValueByUrl($_SERVER['REQUEST_URI']);

There is a lot of best practice design pattern stuff, code style and OOP in my examples, learn about those and you will gain a much higher level of software engineering than the readers of your question. Good luck!

Saturday, May 29, 2021
 
macha
answered 5 Months ago
25

The main thing is that by using the CONSTANT notation, you're making it clear to the reader. the lower case, frozen string gives the impression is might be settable, forcing someone to go back and read the RDoc.

Tuesday, August 10, 2021
 
Optimight
answered 3 Months ago
58

From How To – Persist Rails or IRB Console Command History After Exit.

Create, or edit your ~/.irbrc file to include:

require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 200
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"
Saturday, September 4, 2021
 
talkhabi
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :