Page 1 of 1
calling a function in an external class
Posted: Sat Jan 11, 2003 2:05 pm
by cscrum
I'm very new to PHP and am in need of knowing how I would call a function in an external class. I would like to click on some text and have it call a function in a class on another page that just contains a couple of classes that set some values in an array. How would I do this? I'm guessing it will look something like:
<a HREF="classpage.php" onclick="function(args)" >text </a>
but I don't know the proper syntax and I've looked at several reference materials. If anyone can help I'd appreciate it.
Posted: Sat Jan 11, 2003 6:11 pm
by Elmseeker
You have to include the class file into your php doc and then create an instance of the class object:
Code: Select all
<?php
include("myclass.php");
$myclass = new myclass();
?>
Than you can call the function from within that class, but your example above won't work as is, try:
Code: Select all
<a HREF="classpage.php" onclick="<? function(args); ?>" >text </a>
Hope this helps.

Posted: Sat Jan 11, 2003 6:21 pm
by Next_Gate
It Is like this:
$myclass->function(args);
Of course yo need the code above, the include and all that stuff.. but in the time you call the function in this way
Posted: Sat Jan 11, 2003 6:24 pm
by Elmseeker
Yeah, heh, sorry I thought it didn't look quite right...thanks Next_Gate!
Posted: Sat Jan 11, 2003 6:34 pm
by Next_Gate
jeje.. you dont even imagine how many times i forget the obj-> jejejje
Posted: Sat Jan 11, 2003 7:55 pm
by volka
I would like to click on some text and have it call a function in a class on another page...
<a HREF="classpage.php" onclick="function(args)" >text </a>
onclick is a client-side event and will therefor be evaluated client-side. It will not call any php-function but something like javascript.
You might pass parameters to your server-side php-script by appending them to the url, e.g.
Code: Select all
<a href="classpage.php?function=funcname&arg=1234">text </a>
when your script is invoked that way you can access the parameters via
the superglobal $_GETCode: Select all
<?php
require("myclass.php"); // it's mandatory to have this class
if (isset($_GET['func'])) // something to be invoked?
{
$myclass = new myclass(); // new object
if (method_exists ($myclass, $_GET['func'])) // object has this funtionality?
$myclass->$_GET['func'](isset($_GET['arg']) ? $_GET['arg']:''); // call this function with the passed argument or an empty string if no arg. passed
}
?>