__call()CakePHP的控制器中有功能吗?我在Zend Framework中使用了这个函数.
class UsersController extends AppController {
function home(){
/*some action*/
}
function __call($m, $p){
print_r($m);
print_r($p);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
UsersController中缺少方法
Run Code Online (Sandbox Code Playgroud)<?php class UsersController extends AppController { var $name = 'Users'; function somemethodsnotincontoller() { } } ?>对于URL site.com/users/somemethodsnotincontoller
我正在构建一个小的MVC系统(学习),我在视图文件中显示变量时遇到了一些问题.
这来自我的View类:
private $vars = array();
public function __set($key, $value)
{
$this->vars[$key] = $value;
}
public function __get($key)
{
return $this->vars[$key];
}
public function __toString()
{
return $this->vars[$key];
}
public function show($file)
{
global $router;
$folder = strtolower($router->current_controller);
$path = VIEWPATH.$folder.'/'.$file.'.phtml';
if ( ! file_exists($path))
{
die("Template: $file, not found");
}
include ($path);
}
Run Code Online (Sandbox Code Playgroud)
这是来自我的控制器:
$test = new View();
$test->name = 'karl';
$test->show('name_view');
Run Code Online (Sandbox Code Playgroud)
和视图文件(name_view)
echo $name // doesn't work
echo $this->name // Works
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?也许我想要做一些全球化的事情?
THX/Tobias
编辑:我刚刚在包含视图文件之前在视图类中提取了vars数组,然后它工作了..谢谢大家的帮助.
我不明白为什么__unset()不工作.
class myclass {
public $name = array();
public function __set($arraykey, $value){
$this->name[$arraykey] = $value;
}
public function __isset($argu){
return isset($this->name[$argu]);
}
public function __unset($argu){
echo "Working: Unset $this->name[$argu]";
unset($this->name[$argu]);
}
}
$obj = new myclass;
$obj->name = 'Arfan Haider';
var_dump(isset($obj->name));
unset($obj->name);
Run Code Online (Sandbox Code Playgroud)
我读到只要unset()调用该函数,就会__unset()自动调用Magic Method 并取消设置变量.
在上面的代码我使用unset但它没有调用__unset().为什么?我在理解魔术方法时遗漏了什么__unset()?
我想使用下一个语法:
$o = new MyClass();
$o['param'] = 'value'; // set property "param" in "value" for example
Run Code Online (Sandbox Code Playgroud)
现在我有一个错误:
Fatal error: Cannot use object of type MyClass as array
Run Code Online (Sandbox Code Playgroud)
我可以使用这样的对象吗?也许有任何魔术方法?
我想要滚动我自己的简单对象,可以跟踪变量的单位(也许我会添加其他属性,如公差).这是我到目前为止:
class newVar():
def __init__(self,value=0.0,units='unknown'):
self.value=value
self.units=units
def __str__(self):
return str(self.value) + '(' + self.units + ')'
def __magicmethodIdontknow__(self):
return self.value
diameter=newVar(10.0,'m') #define diameter's value and units
print diameter #printing will print value followed by units
#intention is that I can still do ALL operations of the object
#and they will be performed on the self.value inside the object.
B=diameter*2
Run Code Online (Sandbox Code Playgroud)
因为我没有正确的魔术方法,所以我得到以下输出
10.0(m)
Traceback (most recent call last):
File "C:\Users\user\workspace\pineCar\src\sandBox.py", line 25, in <module>
B=diameter*2
TypeError: unsupported operand type(s) for *: …Run Code Online (Sandbox Code Playgroud) 我试图在python中编写自己的矩阵类,仅用于测试目的.实际上,这个矩阵类是用c ++编写的,我使用SWIG来连接两者.但是,对于这个问题,考虑这个矩阵类的纯python实现可能更简单.
我希望能够调用此矩阵类并使用双索引切片.例如,在我们创建4x4矩阵之后,
>>> A = Matrix(4,4,1)
Run Code Online (Sandbox Code Playgroud)
我希望能够获得sub 2x2矩阵:
>>>> A[1:2,1:2]
Run Code Online (Sandbox Code Playgroud)
我听说过这种__getslice__方法,但这似乎只允许单一切片,例如A[1:2].那么如何才能执行双索引切片以便我可以调用A[i:j,l:k]?
谢谢!
有些框架有自己的魔术方法名称,例如
$player->findByName('Lionel Messi')
Run Code Online (Sandbox Code Playgroud)
这导致简单的SELECT * FROM players WHERE name='Lionel Messi'查询.在PHP中我如何制作类似的方法?他们以某种方式抓住了全球MethodNotFoundException吗?
我有一个课程如下:
class Integer {
private $variable;
public function __construct($variable) {
$this->varaible = $variable;
}
// Works with string only
public function __isString() {
return $this->variable;
}
// Works only, If Im using the class as a function (i must use parenthesis)
public function __invoke() {
return $this->variable;
}
}
$int = new Integer($variable);
Run Code Online (Sandbox Code Playgroud)
我喜欢和变量一样使用类:
$result = $int + 10;
我不知道,我怎么能回来$int;?
我有这部分代码:
class FTPClient
{
public function __construct() {
$args_num=func_num_args();
echo $args_num;
$this->{"__construct".($args_num===0 ? '' : $args_num)}(func_get_args());
}
function __call($name,$args) {
echo $name,count($args),'<br/>';
}
public function open() {
echo 'open';
}
}
$o = new FTPClient('127.0.0.1','user','pass');
$o = new FTPClient();
$o = new FTPClient('127.0.0.1','user');
$o->close();
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:
3__construct31 01__construct11 //Don't understand how this output came together?! 2__construct21 close0
有人会这么善良,可以解释一下这个输出的第二行吗?
比方说,我有一个类,其成员想与通常的运营商比较==,<,<=,>,和>=.
据我所知,这可以通过初始化定义魔术方法来实现,该方法__cmp__(a, b)返回-1(a < b),0(a == b)或1(a > b).
这似乎是__cmp__ 因为Python 3弃用赞成界定__eq__,__lt__,__le__,__gt__,和_ge__单独的方法.
我定义__eq__并__lt__假设默认值__le__看起来像return a == b or a < b.以下类似乎不是这样的:
class PQItem:
def __init__(self, priority, item):
self.priority = priority
self.item = item
def __eq__(self, other):
return isinstance(other, PQItem) and self.priority == other.priority …Run Code Online (Sandbox Code Playgroud)