Template class stops another class from working - php

Trying to get a simple template system working, but it seems to affect another class.
I have two PHP classes, both called in index.php:
HeaderData.php - class allows user to setup Header data, eg doctype,
charset.
PageTemplates.php - class allows user to include a template file.
When I call the HeaderData class, then manually require() a template file from view (ie contains doctype, head, meta tags etc), the code from the header class works fine in the template file, outputting the objects from the header class such as Doctype etc.
However, when I try to use the PagesTemplate class to include the template file (with manual require() removed from index.php), the template file is included, but the HeaderData class no longer works in it.
Error log shows:
Notice: "Undefined variable varHeaderData";
Error: "Call to a member function PageDoctype() on a non-object";
Both the variable and function worked fine when manually require() the template file in index.php.
The code below is dev only (data checks etc to be added):
index.php:
// Use HeaderData Class
$varHeaderData = new HeaderData(
array(
'PageDoctype' => '<!DOCTYPE html>',
'PageCharset' => 'UTF-8',
));
// Use PageTemplates Class
$varPageTemplate = new PageTemplates();
$varPageTemplate->LoadTemplate('template-file.php');
PageTemplates.php Class:
class PageTemplates
{
private $strTemplatesDir = 'view/templates/';
public function __construct($strTemplatesDir = NULL)
{
if ( is_dir($strTemplatesDir) )
{
$this->strTemplatesDir = $strTemplatesDir;
}
}
public function LoadTemplate($strTemplateFile)
{
{
require_once ($this->strTemplatesDir.$strTemplateFile);
}
}
}
template-file.php:
echo $varHeaderData->PageDoctype();
The HeaderData class simply sets the object the user entered in the array with the PageDoctype() function in the class (again this works fine without the page template class)
Can someone tell me why when using the template class to require() the template file, it stops the objects from the HeaderData from working in the template-file.php?

The simplest way to do it is to pass variable in to the LoadTemplate method as an array and use extract function:
public function LoadTemplate($strTemplateFile, $vars = array())
{
extract($vars);
require($this->strTemplatesDir.$strTemplateFile);
}
And in the index.php
...
$varPageTemplate->LoadTemplate('template-file.php', array(
'varHeaderData' => $varHeaderData
));
One of your errors says: "Call to a member function PageDoctype() on a non-object", so make sure that PageDoctype() method is defined in the HeaderData class.

Related

Calling function in PHP class not working

I have a a class stored in path plug/PHPDocumentParser/DocumentParser.php:
namespace LukeMadhanga;
class DocumentParser {
static function parseFromString($string) {
// do stuff
}
}
I want to call the class and function. I run this in a file that's stored at the base folder:
include_once("plug/PHPDocumentParser/DocumentParser.php");
$docObj = new DocumentParser();
$docText = $docObj->parseFromString('hello world');
I receive this error:
Fatal error: Class 'DocumentParser' not found
I am pretty sure the problem is how I call the class, correct?
You are calling static function in wrong way. Try
DocumentParser::parseFromString()
Also use require_once, you will know if it was included correctly. (maybe path is wrong.)
Edit : Ok, you added namespace now - it should be \LukeMadhanga\DocumentParser::parseFromString() thats also why you dont get instance of DocumentParser using new.
Of course you can always add use keyword at top of your file to include your namespace.

Classes - require conflict in constructor

I'm coding a Wordpress plugin and I'm not sure regarding the function name conflict..
I have a file named test_handling.php which contains the following content :
function testing() { echo 'test'; }
I included this file in a class constructor (file named testcls.class.php) :
class TestCls {
function __construct() {
require_once('test_handling.php');
testing();
}
function otherfunction() {
testing();
}
// ...
}
In this case, I would like to know if the testing() function is only available in the TestCls class, or can it create conflicts if an other WP plugin has a function with the same name ?
Even with the same name, the functions will have different scope if defined as class method. To make a call to a regular function you will do the following:
testing();
and the result will be:
'test'
the class method need an instance of the class or be statically called. To call the method class you will need the following formats:
$class->test();
or
OtherPlugin::test();
To sum up, the function test will be different if defined as class method. Then, you will not have conflicts.
Other way to encapsulate your function and make sure you are using the right one is with namespaces. If you use a namespace in your test_handling.php
<?php
namespace myname;
function testing(){echo 'test';}
?>
You will access the function test like this:
<?php
require_once "test_handling.php";
use myname;
echo myname\testing();
Now you are sure about the function you are calling.
When a file is included, the code it contains inherits the variable
scope of the line on which the include occurs. Any variables available
at that line in the calling file will be available within the called
file, from that point forward. However, all functions and classes
defined in the included file have the global scope.
from include in PHP manual
Which means that yes, you can have conflicts.

How to fix this PHP error?

I have a header method that shows in the top of a page, it is inside a class, inside my header() method I run this code here to start a new Profiler object...
//start new page timer object
$profiler = new Profiler;
$profiler->start();
After a bunch of other files are compiled, I then include a file into the footer section, in this file I run this code,
echo 'Page Generated in ' .$profiler->end(). ' of a second with ' .$_SESSION['querie_counter']. ' MySQL Queries';
However I am getting this error message in the footer file now,
Notice: Undefined variable: profiler
in
C:\webserver\htdocs\friendproject2\includes\footer.inc.php
on line 21
Fatal error: Call to a member function
end() on a non-object in
C:\webserver\htdocs\friendproject2\includes\footer.inc.php
on line 21
How can I fix this?
If you created the $profiler object inside the header method, it will not be available in another method, unless it was a global $profiler was a global variable or it was a singleton.
To make it global, declare $profiler outside the header method and then inside the header method, include this line:
global $profiler;
Include this line in the the footer method as well. The rest of your code can stay the way it is. It should work.
Variables created in a function are local to that function. Use the global keyword to declare a global variable.
I'd suggest make the $profiler a property of the main class and initialize it inside the constructor method, since it's not really related to the header.
Assuming this is the main class
class Example {
private $profiler;
public function __construct() {
$this->profiler = new Profiler;
}
public function header() {
...
}
}
Inside the included header, initialize this object and run header, and just to make it more explicit, make the profiler start call separately:
$example = new Example();
$example->profiler->start();
$example->header();
Inside the included footer:
$example->profiler->end();
Possible Steps:
First of all make sure that you include the file which contains the profiler class.
If you are using the profiler instance inside a function, you need to use the global keyword:
global $profiler;
I Think if you remove session() from the top of your header your problems will be fixed ...

Can I have a class method include a header file for me in PHP?

Please review the example code below, I have a class file that is loaded into a config file. The config file is then loaded into any page I build. Is it possible to include a header file the way I have in the show_header() method? It doesn't seem to work so how can I achieve this result?
// Core.class.php
class Core
{
public function show_header($page_name){
require_once 'includes/header.inc.php';
}
}
// config.inc.php
require_once 'Core.class.php';
$core = New core;
// testpage.php
require_once 'config.inc.php';
$core->show_header('home');
Here is the top part of the header.inc.php file I am trying to include into the page, it seems to work including it but it breaks the way the header file works.
//header.inc.php
<?PHP
//start page timer
$session->get('user_id');
$profiler = new Profiler;
$profiler->start();
//see if site is turned on/off
$core->sitestatus($config['site_status']);
This part gives me errors like this...
Notice: Undefined variable: session in
C:\webserver\htdocs\friendproject2\includes\header.inc.php
on line 5
Fatal error: Call to a member function
get() on a non-object in
C:\webserver\htdocs\friendproject2\includes\header.inc.php
on line 5
When you're including a file from within a function it's just as if you wrote the code within that file from within that function.
e.g.
file foo.php:
<?php
echo $foo->getFoo();
file bar.php
<?php
class Foo {
public function getFoo() {return 'foo';}
}
$foo = new Foo();
function bar()
{
require 'foo.php';
}
bar();
The above will result in the following notice/error being thrown, because $foo is not known within bar().
Fatal error: Call to a member function getFoo() on a non-object in /Users/hobodave/foo.php on line 3
Edit:
I'm not sure what your "Core" class fully entails, but you could perhaps use it as a type of storage for your "globals".
e.g.
<?php
$session = new Session();
$core->session = $session;
Then your $session would be accessible in your header using $this->session
re your comment, sounds like you need a root web context object that you reference the other objects from:
$ctx = WebContext::get();
$ctx->session->get('x');
$ctx->input->get('y');
$ctx->identity->valid;
etc... this is how most web frameworks do it.
$session would need to be defined, then referenced in the included file:
// If a global variable:
global $session;
$session->get('x');
// If a member of Core:
$this->session->get('x');
yes you can do that, probably you'll want require instead of require_once, and the paths would need to be based on the current working directory or an absolute path
try adding error_reporting(E_ALL) to see if any notices are happening...
All calls you make inside the header file will be called as if they were local calls inside the show_header function. So if you want to use any global variable, you will have to use global $variablename; on the top of the included file (or in the beginning of the show_header function).
If you use a static function for the session class you wouldn't need to define it in the same file. http://php.net/manual/en/language.oop5.static.php
You are trying to access $session which is out of scope as pointed in another answer.
Since session stuff is usually global throughout most apps consider using the singleton pattern for the Session class.
This way you can do something like $session = Session::getInstance().
This lets you use the session class anywhere and you usually only one need one instance of a session class (usually). Take a look at Zend Framework for examples on singleton classes.

How can I initiate a PHP class and use it in several files?

I am stumped right now. In my last post about this question the answer was to use a singleton to make sure an object is only initiated 1 time but I am having the opposite problem.
If I have a file called index.php and then I include these files into it, class1.php, class2.php, class3.php, class4.php.
In index.php I will have,
<?PHP
$session = new Session();
require_once '/includes/class1php';
require_once '/includes/class2.php';
require_once '/includes/class3.php';
require_once '/includes/class4.php';
?>
then in all 4 of the test files I will try to access a method called get() from the session class, assume the session class file is already included into the index.php page as well.
Now if I try to use...
$testvar = $session->get($var1);
in any of the test class files I will get this error
Fatal error: Call to a member function get() on a non-object
the only way the code works without an error is if I use
$session = new Session();
in every file.
How can I fix/avoid having to initaite the class in every file when it is already initated in the index.php file?
the goal is to let me initiate a class in 1 file like index.php and then include the class files into that page, the catch is most of the classes use methods from other classes so would be nice if I didn't have to initiate every class in every file
Without seeing the code it's hard to tell, but I think I can make some assumptions. correct me if I'm wrong:
EDIT: So post your source so we can stop speculating
1) The files you are including are class files. in other words, they contain something like:
class a
{
function a(){}
function b()
{
}
}
2) You aren't trying to execute code in the class files, at load time, but at some later time by instantiating them
i.e.
require("class.a.php");
$myA = new a();
$a->b();
If you are trying to reference your session variable inside those classes, then you have a scope issue. A variable declared outside a class definition can't be used inside the class, unless it is declared as a global var inside the class.
class a
{
function a(){}
function willFail()
{
$session->doSomething(); //fails
}
function b()
{
global $session;
$session->doSomething(); //succeeds
}
}
Even then, you probably don't want to do that, but instead you should pass in your session as a variable if the class needs access to it:
class a
{
function a(){}
function b($session)
{
$session->doSomething(); // yay!
}
}
You could have a base class they all all extend from
Example
class test1 extends Base {
public function doSomething() {
$this->session->get('something');
}
}
class Base {
protected session;
public function __construct() {
$this->session = new Session();
}
}
You're kind of thinking about it backwards. Any file that will use the session object will need to include the file containing that class definition. The alternative is to use __autoload to pull the class in:
function __autoload($classname)
{
if ($classname == 'Session')
{
include_once 'Session.php';
}
}
EDIT : you'll need to put the file containing that autoload into every file that will use it.

Categories