Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP calling function within a class.

12:41 pm on May 30, 2014 (gmt 0)

Here is the code what i do, To call a function within a class,
Which works fine, But i want to know that is there any other way to Call function within a class?
class demo {
public function demo_one(){

private function demo_big(){
// Code for demo_big

private function demo_small(){
// Code for demo_small

public function demo_test(){
// Code for demo_test

$demoObject = new demo();


1:02 pm on May 30, 2014 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You are trying to call a private function (method) from outside the parent class itself and that will throw a FATAL ERROR. If you truly need the ability to call that function from the "outside" then you can merely change it to a public function. More reading here:
6:19 am on May 31, 2014 (gmt 0)

hmmh ... it works or. so you want just know if there are other ways to achieve the same?

1. static methods

public function demo_one(){
// $this->demo_big();
// $this->demo_small();


private static function demo_big(){
echo 'demo_big';

private static function demo_small(){
echo 'demo_small';

2. call_user_func

public function demo_one(){
// $this->demo_big();
// $this->demo_small();

$methodsToCall = array('demo_big', 'demo_small');
foreach($methodsToCall as $methodName) {
if (method_exists($this, $methodName)) {
call_user_func(array($this, $methodName));
8:01 am on Jun 6, 2014 (gmt 0)

Thanks for your suggestion, Will try it...

Featured Threads

Hot Threads This Week

Hot Threads This Month