Need help in best practice with Classes and Methods
Posted: Sat Sep 06, 2014 10:38 am
I have a program set that I'm trying to "retrofit" to better practices. To summarize, I originally used a php include that just consisted of a bunch of functions that did things like create the HTML at the top of a page, retrieved certain data from a database, did assessments of data relationships and returned a set value, etc.
Several of the functions were simple things that did not require parameters and did not return them. For instance the following function to place at the top of each page
I would just include the main_fns.php include on each page, then call this function with like main_page_header("Welcome to the Main Site"); type of statement.
I now want to replace this main_fns.php with a Class that includes various public functions. My biggest problem, though, is how exactly to invoke that same method in my code. Here's a portion of what I have:
In my main line of code I simply require_once the php class, then instantiate it with $fi = new FileInfo(); statement.
However when I go to use the function with $fi->pageStart("Welcome to the Main Site"); nothing happens. In fact, the program acts as if it has errors but none are reported.
Can someone point me in the right direction on how to make this work?
Thanks!
Several of the functions were simple things that did not require parameters and did not return them. For instance the following function to place at the top of each page
Code: Select all
function main_page_header($page_title) {
echo '<!DOCTYPE HTML>';
echo '<html>';
echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">';
echo '<title>' .$page_title. '</title>';
echo '<link href="css/main.css" rel="stylesheet" type="text/css" />';
echo '</head>';
echo '<body>';
}I would just include the main_fns.php include on each page, then call this function with like main_page_header("Welcome to the Main Site"); type of statement.
I now want to replace this main_fns.php with a Class that includes various public functions. My biggest problem, though, is how exactly to invoke that same method in my code. Here's a portion of what I have:
Code: Select all
<?php
require_once ('MySQL.php');
Class FileInfo {
public function pageStart($page_title) {
echo '<!DOCTYPE HTML>';
echo '<html>';
echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">';
echo '<title>' .$page_title. '</title>';
echo '<link href="css/main.css" rel="stylesheet" type="text/css" />';
echo '</head>';
echo '<body>';
}
public function pageEnd() {
echo '</body>';
echo '</html>';
}
However when I go to use the function with $fi->pageStart("Welcome to the Main Site"); nothing happens. In fact, the program acts as if it has errors but none are reported.
Can someone point me in the right direction on how to make this work?
Thanks!