I am new to PHP classes and I am wondering how I can call a function which resides in the same class I am calling it from. Is this right or the best approach?
page1.php:
$object=new MyClass();
$object->func1();
MyClass.php:
class MyClass{
function func1(){
....
$object->func2();
}
function func2(){
....
}
}
The reason I want to do this is because func1 will call func2 at the end of func1 but I will also need to use func2 independently of func1 in other circumstances and I thought if this is possible it will cut down on the code(even though it would be a simple copy/paste from func2 and place it at the end of func1).
Edit: or could I use:
self::func2();
You can use $this to refer to the current object, and thus call $this->func2().
(The value of $this is automatically populated by PHP when you're in an object instance's context.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With