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.
calling a function in an external class
Moderator: General Moderators
You have to include the class file into your php doc and then create an instance of the class object:
Than you can call the function from within that class, but your example above won't work as is, try:
Hope this helps. 
Code: Select all
<?php
include("myclass.php");
$myclass = new myclass();
?>Code: Select all
<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.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>
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>Code: 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
}
?>