Using functions in classes
Date: 12/22/05
(PHP Community) Keywords: php, mysql, html, sql, web
I am learning classes (trying to anyway).
I have class called photo. It contains two functions:
create_folder()
sterilize_text()
In the create_folder function I want to clean user uploaded data before creating a folder based on that data. I have a few special rules so I wanted to put this in a seperate function instead of regexing all over my pages.
I read that using functions within functions in classes is easy, but I don't know how and the tutorial I'm using doesn't touch on using nested functions.
So what happens is when I invoke the class and use create_function(), php exits with this:
Fatal error: Call to undefined function: sterilize_text() in /var/www/html/includes/classes.php on line 34
Line 34 is : $brand = sterilize_text($myrow[brand]);
Is there a way to code this so I can use functions within functions, inside of my class? I've included the relevant scripts below.
Thank you.
class photo
{
var $model;
// mk the folder for an uploaded image.
function create_folder($model)
{
global $dbx, $dbxold;
if (!isnum($model)||strlen($model)>5) {return(false);}
$q = "SELECT brand FROM spec WHERE model='$model'";
$result = mysql_query($q,$dbxold);
$myrow = mysql_fetch_array($result);
$brand = sterilize_text($myrow[brand]);
if ($brand=="") {return(false);}
$webpath = "/images/dirtbikes/$brand";
$uploaddir = $_SERVER["DOCUMENT_ROOT"] . $webpath;
if (!file_exists($uploaddir)) {
mkdir ($uploaddir, 0777);
chmod($uploaddir,0777);
if (!file_exists($uploaddir)) {
echo "unable to create directory. :(
";
return(false);
}
}
$dir_array[] = $webpath;
$dir_array[] = $uploaddir;
return $dir_array;
}
function sterilize_text($text)
{
$hyphen = "_";
$dash = "-";
$text = strtolower($text);
$text = ereg_replace('[^a-zA-Z0-9_\\,]', '', $text);
$text = ereg_replace('[\\]', $dash, $text);
$text = ereg_replace('[,]', $hyphen, $text);
return $text;
}
}
include ("$_SERVER[DOCUMENT_ROOT]/includes/classes.php");
$dir_array = $c_photo->create_folder($model);
echo "";
print_r($dir_array);
echo "
";
?>
Source: http://www.livejournal.com/community/php/382729.html