Cli*_* C. 1 php arrays class object
我正在为我的网站设置一个搜索类,我遇到了一些事情,我不知道为什么它不起作用.
这是班级
<?php
class search
{
public $location = array();
public $keywords = array();
/*
construct method
accepts $location and $keywords
*/
public function __construct($location, $keywords='')
{
$this->location = $this->format_location($location);
}
/*method format_location
accepts location value, determines its type... city, state, zip code
returns array of properly formatted location values
*/
public function format_location($input)
{
// check if location contains a zip code
$zip_pattern = "/\d{5}/";
$str = $input;
preg_match($zip_pattern,$str,$regs);
// if we do have a zip code lets put it in the location array
(!empty($reg[0])) ? $this->location['zip'] = $reg[0] : '';
// if we don't have a zip let's check for one of our pre=formatted locations, this will avoid unessesary regular expressions and database calls
if(!$this->location['zip']) {
$swinput = strtolower($input);
switch($swinput) {
case "chicago, il":
echo 'hello';
$this->location[] = array('zip' => 60601,
'city' => 'Chicago',
'state' => 'IL',
'lat' => '41.8781136',
'lon' => '-87.629798',
);
break;
case "naperville, il":
$this->location[] = array('zip' => 60564,
'city' => 'Naperville',
'state' => 'IL',
'lat' => '41.785863',
'lon' => '-88.147289',
);
break;
}
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
然后这就是我的称呼方式
require("lib/search/class.search.php");
$lo = new search('chicago, il');
print_r($lo->location);
Run Code Online (Sandbox Code Playgroud)
问题是没有在location属性中设置任何内容.有任何想法吗?
尝试使用
/*
construct method
accepts $location and $keywords
*/
public function __construct($location, $keywords='')
{
$this->format_location($location);
}
Run Code Online (Sandbox Code Playgroud)
代替
$this->location = $this->format_location($location);
Run Code Online (Sandbox Code Playgroud)
因为format_location不返回任何内容.