在php中初始化类中的私有数组

时间:2013-09-12 16:43:48

标签: php arrays oop initialization

是的...我正在创建一个类,我想使用预定义(即const)数组作为私有类成员但是php没有加载数组,即当我在构造函数中尝试array_flip时它告诉$ fieldnamemap为空

class RETS_translate {

      // hold the datebase connection that gets passed in constructor
      private $dbConn;

      // IMPORTANT! this is the bridge between old system and new system...handle with care
      private $fieldNameMap = array(

        "StreetNumber" => "street_number",
        "StreetName" => "street_name",
        "StreetSuffix" => "street_suffix",
        "City" => "city",        
        "StateOrProvince" => "state_province",
        "PostalCode" => "postal_code", 
        "YearBuilt" => "year_built",
        "PropertyType" => "property_type",
        "SqFtLivingArea" => "square_footage",
        "Bedrooms" => "bedrooms" ,
        "BathsTotal" => "bathrooms",
        "PoolPresent" => "pool",
        "WaterFrontPresent" => "waterfront",
        "WaterFrontageDesc" => "water_type",
        "Parking" => "parking",  
        "SplitYN" => "Spli,Floorplan",
        "HomeOwnersAssocYN" => "hoa",
        "AssociationFee" => "hoa_dues",
        "Construction" => "construction",
        "ExteriorFinish" => "exterior_finish",
        "Roof" => "roof_type",       
        "FireplacesYN" => "fireplace",
        "County" => "county",  
        "Gates" => "gated_community",
        "FurnishingstoStay" => "furnishing",
        "HomeWarrantyYN" => "home_warranty",
        "TaxYear" => "tax_year",     
        "TaxAmount" => "tax_amount",
        "Community55YN" => "over_55",
        "ShortSaleYN" => "Short Sale/Bank Owned",
        "DwellingStyle" => "home_style",
        "PublicRemarks" => "remarks",
        "ExteriorFeatures" => "exterior_features",
        "InteriorFeatures" => "interior_features",
        "PoolDescription" => "pool_features",
        "Utilities" => "utilities",  
        "EquipmentAndAppliances" => "equipment_appliances",
        "Floor" => "floor",          
        "Subdivision" => "subdivision",    
        "DwellingView" => "home_view", 
        "AdditionalRooms" => "additional_rooms",

        );  

        private $fieldNameMapFlip;                                   

      function __construct($inDb=NULL) {

        // store db connection for later use...
        $this->dbConn = $db;

        $fieldNameMapFlip = array_flip($fieldNameMap);

      }  // end constructor 

    }

2 个答案:

答案 0 :(得分:0)

由于它是该类的一部分,因此您需要使用$this访问该数组。改变

$fieldNameMapFlip = array_flip($fieldNameMap);

$fieldNameMapFlip = array_flip($this->fieldNameMap);

答案 1 :(得分:0)

$ fieldNameMapFlip = array_flip($ this-> fieldNameMap);