Jump to content

making a subfunction in a class


benphelps

Recommended Posts

Im trying to create a class for this project of mine.

 

This is what it looks like now

 

class myclass {
function myfunction(){	
}	
}

I use this like I should

$class = new myclass;
$class->myfunction();

I want to make a subfunction of "myfunction", so I can use it like this.

$class = new myclass;
$class->myfunction->subfunction();

 

How would I go about doing that ?

Link to comment
https://forums.phpfreaks.com/topic/155939-making-a-subfunction-in-a-class/
Share on other sites

No, then I would have to call it like this.

 

$class = new myclass;
$class->subfunction();

 

and not like this

$class = new myclass;
$class->myfunction->subfunction();

 

I have tried to do an extend class, and I cant call it through the function myfunction.

<?php
error_reporting(E_ALL);
class subClass {
public $test;

function __construct($test) {
	$this->test = $test;
}
public function subMethod( ) {
	echo $this->test;
}
}

class mainClass {
public $subClass;
public $foo;

function __construct( ) {
	$this->subClass = new subClass('hello');
}
}
$myClass = new mainClass;
$myClass->subClass->subMethod();
?>

Its basically making the variable subClass a new class, which allows access to its methods. Whats the purpose for this?

1. It looks really nice and easy to use for people who might not know alot about php.

2. I have alot of functions in the base class, and I want to extend them in categories.

 

Like this:

$administration->account->create($somevars);
$administration->account->downgrade($somevars);
$administration->account->ban($somevars);
$administration->account->delete($somevars);
$environment->announce->toplayer($somevars);
$environment->announce->toworld($somevars);

 

EDIT:

Much thanks for your help, works perfect.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.