简体   繁体   中英

How to assign multiple variables inside a class?

How can I assign multiple variables at once in PHP? This is basically what I'm trying to achieve but it throws a parsing error on private $from = private $to saying syntax error, unexpected 'private' :

<?php
namespace Library;

class Localize
  {
  // Default data for both fields. Localization must be specified.
  private $from = private $to = array(
    'language' => 'en',     // Language: English
    'country'  => 'usa',    // Country: USA
    'currency' => 'dollar', // Currency: dollar $
    'units'    => 'is',     // Units: International System[meter, kelvin, etc]
    'timezone' => '-5'      // Timezone: UTC -5 hours [America/New_York]
    );

  // ...
  }

I've tried private $from = private $to and private $from = $to but both would throw an error. What's the proper syntax for assigning multiple properties at once inside a PHP class?

Note: I know there are many ways of dealing with this like the constructor, but I'd like to know if what I'm trying to achieve exactly is possible or not.

Why would you like to do it that way? Think of a class as a pattern of an entity. This entity usually does not define any data. Data is handled with at the time you create a concrete object of your class. You usually assign values to your class members in the constructor of the class. So I would do the initialization in the constructor.

Php syntax doesn't allow that, but you can do that in constructor:

public function __construct() {
    $this->to = clone $this->from;
}

A better idea would be to use a single array to store all the properties of the class

private $_arLoc = array();

Then, assign values in constructor

$_arLoc[ 'from' ] = array(
    'language' => 'en',     // Language: English
    'country'  => 'usa',    // Country: USA
    'currency' => 'dollar', // Currency: dollar $
    'units'    => 'is',     // Units: International System[meter, kelvin, etc]
    'timezone' => '-5'      // Timezone: UTC -5 hours [America/New_York]
);

$_arLoc[ 'to' ] = $_arLoc[ 'from' ];

In short, no, you cannot. You will have to define them at instantiation.

Does this help?Depends on what you want to use it for.

class Localize
    {
    // Default data for both fields. Localization must be specified.


    private $to = array(
    'language' => 'en',     // Language: English
    'country'  => 'usa',    // Country: USA
    'currency' => 'dollar', // Currency: dollar $
    'units'    => 'is',     // Units: International System[meter, kelvin, etc]
    'timezone' => '-5'      // Timezone: UTC -5 hours [America/New_York]
    );
    private $from ;

    function __construct(){
            $this->from = $this->to;
    }
    function __destruct(){

    }

    // ...
}

$var = new Localize();

var_dump($var);

I think you are already saying the $to array is private!

Try: private $from = $to = array(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM