[英]call a function from inside a function php oop
I wonder if this is possible, although I'm quite convince maybe there is a better approach for this. 我想知道这是否可能,尽管我非常确信也许有更好的方法。 I have this script structure:
我有这个脚本结构:
class Mother {
public function __construct() {
// script here
}
public function writer() {
if() {
// if true
} else {
// call function hello
}
}
public function hello() {
echo "Hello there.";
}
}
How can I call hello() from writer()? 如何从writer()调用hello()? Thanks.
谢谢。
Like so 像这样
public function writer() {
$this->hello();
}
$this
is a reserved variable for classes, any class that is instantiated (called via new myClass
) has access to $this
, however if you're using a static class, you would need to define that function as static and use the static::myFunction
approach, for example: $this
是类的保留变量,任何实例化的类(通过new myClass
调用)都可以访问$this
,但是,如果您使用的是静态类,则需要将该函数定义为static并使用static::myFunction
方法,例如:
class exampleClass {
public static function exampleFunc() {
static::hello();
}
public static function hello() {
echo "Hello!";
}
}
exampleClass::exampleFunc();
// call function hello
$this->hello();
Also, your other functions are syntactically wrong. 另外,您的其他功能在语法上也是错误的。 Note the parenthesis.
注意括号。
public function writer() {
public function hello() {
on my PHP 5.3.4 installation 在我的PHP 5.3.4安装上
public function hello() { }
seems to to available from another instance method in two ways 似乎可以通过两种方式从另一个实例方法获得
$this->hello()
self::hello()
Obviously, 明显,
$this
the reference to the instance will not be available when calling a public method as a class method 当将公共方法作为类方法调用时,实例的引用将不可用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.