Hi I'm trying to make my own simple template system kind of thing
and I'm just learning about classes so I'm trying to do it with classes and objects.
it works if i put this in top of every document:
$template = new Includes('name', 'path');
$include = new Includes('name', 'path');
but it feels like it shouldn't be necessary, and its not that pretty.
This is how my code is arranged right now :
index.php:
<?php
require_once 'class_include.php';
$template->loadTemplate('body');
body.php:
<?php require_once 'class_include.php'; ?>
<head>
<?php $template->loadTemplate('head'); ?>
</head>
<body>
<?php
$template->loadTemplate('sidepanel');
$template->loadTemplate('content');
?>
</body>
class_include.php:
class Includes {
public function loadTemplate($name, $path = 'template'){
require_once "$path/$name.php";
}
public function loadInc($name, $path = 'inc'){
require_once '$path/$name' . '.php';
}
}
$template = new Includes('name', 'path');
$include = new Includes('name', 'path');
error message:
( ! ) Fatal error: Call to a member function loadTemplate() on a non-object in C:\wamp\www\project\template\body.php
( ! ) Notice: Undefined variable: template in C:\wamp\www\project\template\body.php
Thanks for any help you can provide!
Think that in your code, you should use static methods instead of objects.
index.php
<?php
require_once 'class_include.php';
Includes::loadTemplate('body');
body.php
<head>
<?php Includes::loadTemplate('head'); ?>
</head>
<body>
<?php
Includes::loadTemplate('sidepanel');
Includes::loadTemplate('content');
?>
</body>
class_includes.php
class Includes {
public static function loadTemplate($name, $path = 'template'){
require_once "$path/$name.php";
}
public static function loadInc($name, $path = 'inc'){
require_once '$path/$name' . '.php';
}
}
Related
I'm new in Facebook SDK and I'm trying to load my header and footer (class.Header.incand class.Footer.inc are in the same folder as index page) PHP classes with the following code
gives this error
Fatal error: Class 'Footer' not found C:\wamp64\www\...
https://raw.githubusercontent.com/sohaibilyas/facebook-php-sdk-v5/master/login-on-website-get-basic-info.php
<html>
<head>
...
<?php
function __autoload($class_name)
{
include 'class.' . $class_name . '.inc';
}
<!-- Header -->
$header = new Header();
echo $header->display(basename($_SERVER['PHP_SELF']));
?>
...
<?php
session_start();
require_once '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
.....
echo 'Log in with Facebook!';
}
.... html ....
<!-- Footer -->
<?php
$footer = new Footer();
echo $footer->display();
?>
I don't have an explication of why the function __autoload($class_name) isn't loading up the footer class
I've got two files, like these two:
<?php
session_start();
class test{
public function login($code){
$app = JFactory::getApplication('site');
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select(array('id'))
->from($db->quoteName('#__users'))
->where($db->quoteName('code') . " = " . $db->quote($code));
$db->setQuery($query);//execute query
$results = $db->loadObjectList();//get results
if ($results[0] != null) {
$this->createSession($results[0]);
header("refresh:2;url=second_file.php");
}
return json_encode($results);
}
public function createSession($LoginInfo){
$_SESSION['userId']=$LoginInfo->id;
}
}
?>
and another php file like this:
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<?php
echo $_SESSION['userId'];
?>
</body>
</html>
In the second page seems like that there is no information inside the session and i'm unable to get "userId" parameter.
PS: the first class php file is called by another php file where there is something like this:
require_once('test.class.php');
$mainframe = new test
if ($_GET['code']!=null) echo $mainframe->login($_GET['code']);
Where am I doing wrong? thanks.
PPS: All those file are hosted inside altervista.org and there is Joomla CMS installed.
I have been using Classes in PHP.
I have a directory, test, In which there is index.php
A sub-directory to test is new, which has checkuser.php
Code for checkuser CODE:
<?php
public class checkuser{
public function checkuser()
{
echo "This is class";
}
}
?>
Code for index.php:
<?php include('new/checkuser.php'); ?>
<html>
<head>
</head>
<body>
<?php
checkuser::checkuser();
?>
</body>
</html>
But It always throws error.
Please Help.
What kind of error is that?
There is more things:
You are declaring the class "public"
Parse error: syntax error, unexpected T_PUBLIC on line 2
You are calling non-static method statically
Fatal error: Non-static method checkuser::checkuser() cannot be called statically on line 17
You are including the file, without being aware of the context, i suggest this practice:
<?php include( dirname(__FILE__) . '/new/checkuser.php'); ?>
or if you are using PHP > 5.3
<?php include( __DIR__ . '/new/checkuser.php'); ?>
And here is how your code could work:
<?php
class checkuser {
public static function myfunction()
{
echo "This is class";
}
}
?>
<html>
<head>
</head>
<body>
<?php checkuser::myfunction(); ?>
</body>
</html>
http://codepad.org/C9boO1lI
Note: of course, you can split your code in more files, just make sure that you specify the path as seen above, so it can find the actual file
I'm developing a website for my project.
My folder structure looks like this:
/testingSite
/src
/WordBreaker.php
/index.php
I have a problem when using a function in WordBreaker.php
Here is how the function is defined in WordBreaker.php
<?php
class WordBreaker
{
function breakIntoWords($text)
{
$ranges = $this->breakIntoRanges($text);
$textList = $this->rangesToTextList($text, $ranges);
return $textList;
}
}
?>
And here is where I want to use a function (in index.php)
<?php
include "src/WordBreaker.php";
$instance = new WordBreaker ();
if ( isset( $_POST['btnSubmit'] ) ) {
$result = $instance->breakIntoWords($input);
}
?>
When I test the site, the error occur
Fatal error: Class 'WordBreaker' not found in C:\xampp\htdocs\testingSite\index.php on line 4
What should I do?
Use require_once instead of include
<?php
require_once("src/WordBreaker.php");
$instance = new WordBreaker ();
if ( isset( $_POST['btnSubmit'] ) ) {
$result = $instance->breakIntoWords($input);
}
?>
or you can add an autoloader function
<?php
function __autoload($class_name) {
require_once $class_name . '.php';
}
$vars = new IUarts();
print($vars->data);
?>
You can include it using DIR
require __DIR__."/src/WordBreaker.php";
$instance = new WordBreaker();
_ DIR _ is 'magical' and returns the directory of the current file without the trailing slash.
" If used inside an include, the directory of the included file is returned. This is equivalent to dirname(_ FILE _). This directory name does not have a trailing slash unless it is the root directory."
Good day.
I have problems when i use globals element in some files which include in one file.
Structure my files you can see down:
Files:
-index.php
--function.php
--globals.php
--lang.php
--allfunction.php
Code all pages you can see down:
Code index.php:
<?
session_start();
require_once("./function.php");
select();
?>
Code function.php:
<?php
require_once("./globals.php");
require_once(dirname(__FILE__)."/lang.php");
include_once Language(3);
require_once(dirname(__FILE__)."/allfunction.php");
?>
Code globals.php:
<?
$dirang = './';
$langfile = 'lang.php';
$test = 'hello';
}
?>
Code lang.php:
<?
Language($rem){
return $GLOBALS["langfile"]; //ex.
}
?>
Code allfunction.php:
<?
echo $GLOBALS["test"]; //ex.
}
?>
I get problrem when i use $GLOBALS["test"] in allfunction.php.
I get error Undefened index test in allfunction.php on line ....
Tell me please why i get it error and how aright use global element in allfunction.php ?
you should write globals inside file where you use theys, for ex. if globals you use in allfunction.php you must write global variables in this file(in which you use them)
Not use a separate file with global variables.
Don't use globals. Store your variables in static Config or Registry class instead. Also have a read about singleton design pattern.
Example:
config.php
<?php
class Config
{
static $var1 = '...';
static $var2 = '...';
public static function init()
{
self::$var2 = 1+1; //expressions go here
}
}
Config::init();
usage.php
<?php
require_once 'config.php';
function x()
{
$someKindOfSetting = Config::$var1;
}