通常,如果我想将$ myarray中的参数传递给$ somefunction,我可以在php中使用
call_user_func_array($somefunction, $myarray);
Run Code Online (Sandbox Code Playgroud)
但是,当希望调用的函数是对象的构造函数时,这不起作用.出于相当明显的原因,它无法做到:
$myobj = new call_user_func_array($classname, $myarray);
Run Code Online (Sandbox Code Playgroud)
有什么相当优雅的确有效吗?
我所有的django模型都有unicode函数,目前这些函数往往是这样编写的:
def __unicode__(self):
return u'Unit: %s -- %s * %f' % (self.name, self.base.name, self.mul)
Run Code Online (Sandbox Code Playgroud)
但是,Code Like a Pythonista,在http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#string-formatting指出这self.__dict__是一本字典,因此上面的内容可以简化为:
def __unicode__(self):
return u'Unit: %(name)s -- %(base.name)s * %(mul)f' % self.__dict__
Run Code Online (Sandbox Code Playgroud)
这是有效的,除了"base.name",因为python尝试查找self.__dict__['base.name']哪个失败,而self.base.name工作.
即使您需要遵循外键关系,是否有一种优雅的方式来完成这项工作?
在这里的Leaflet文档中:http://leafletjs.com/reference-1.2.0.html#circlemarker它说CircleMaker扩展了Circle,并且它是相同的,除了半径以像素而不是以米为单位指定,即使缩放地图,圆圈也保持不变.
但我需要Circles,因为我试图在地图上绘制100米半径的圆圈.为此,我使用以下代码:
var geojsonLayer = new L.GeoJSON(null,{
pointToLayer: function (latlng){
return new L.CircleMarker(latlng, {
radius: 5,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8,
});
}});
map.addLayer(osm);
map.addLayer(geojsonLayer);
geojsonLayer.addGeoJSON(jsonExample);
Run Code Online (Sandbox Code Playgroud)
这很好用,但是,如果我将代码更改为使用"Circle"而不是CircleMaker,则整个地图无法加载,并且我收到一个javascript错误:
Error: Error: Invalid LatLng object: (56.229917, NaN)
Run Code Online (Sandbox Code Playgroud)
我可以通过预过滤geojson来解决这个问题,去掉那些缺乏纬度和经度的点,但我很困惑:Circle和CircleMaker都指定他们将LatLng对象作为中心点的规格,我不要不知道某个LatLng对象如何作为CircleMarker的中心点有效,但如果用作Circle的中心点则无效.
我是否忽视了一些明显的东西,或者这只是Leaflet中的一个弱点和/或错误,我只需要解决这个问题?
arguments ×1
arrays ×1
constructor ×1
django ×1
foreign-keys ×1
function ×1
geojson ×1
javascript ×1
leaflet ×1
php ×1
python ×1