Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please I am new to PHP environment.
I want to create a Template.php class but i am getting an error

What I have tried:

//here is my Template.php file inside a lib folder

$template = $template;
}
public function __get($key)
{
return $this->$vars[$key]; // am getting undefined variable
}
public function __set($key, $value)
{
return $this->$vars[$key] = $value; // I am getting undefined variable
}
public function __toString()
{
extract($this->$vars);
chdir(dirname($this->template));
ob_start();

include basename($this->template);
return ob_get_clean();
}
}

?>


// here is my init.php
Posted
Updated 10-Jul-20 23:10pm
Comments
Richard MacCutchan 10-Jul-20 9:39am    
You have not defined $vars anywhere.
Codeignite 10-Jul-20 9:49am    
I notice that but i have modified the code.

I am still getting error messages
public function __toString()
{
extract($this->vars, EXTR_PREFIX_SAME, "dup");
chdir(dirname($this->template));
ob_start();
include basename($this->template);
return ob_get_clean();
}
// error messages in output
Warning: chdir(): Invalid argument (errno 22) in C:\xampp\htdocs\joblisting\lib\Template.php on line 25

Warning: include(): Filename cannot be empty in C:\xampp\htdocs\joblisting\lib\Template.php on line 27

Warning: include(): Failed opening '' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\joblisting\lib\Template.php on line 27
Richard MacCutchan 10-Jul-20 9:55am    
That is different code, and it would appear that $this->template is not returning a valid directory name. You need to debug your code and check exactly what values are being returned in the variables.
Codeignite 10-Jul-20 17:58pm    
@Richard MacCutchan I am new to PHP I have printed the value it returns using var_dump(), it returns bool(false)
Richard MacCutchan 11-Jul-20 5:17am    
Please use the Improve question link above, add the actual code that you are using and the output you see. And please use <pre> tags around your code so it is clear and readable.

1 solution

You have not declared $var in your class which is why it cannot be found as referenced.

Try the following -
PHP
<?php

  class MyClass {

    private $vars;

    public function __construct(Array $properties=array()){
      $this->vars = $properties;
//No dollar sign...
    }

    // magic methods!
    public function __set($property, $value){
      return $this->vars[$property] = $value;
    }

    public function __get($property){
      return array_key_exists($property, $this->vars)
        ? $this->vars[$property]
        : null
      ;
    }
  }

?>


Usage will be the same -

PHP
// init call to the class name...
$foo = new MyClass(array("hello" => "world"));
$foo->hello;          // => "world"

// set: this calls __set()
$foo->invader = "Codeignite";

// get: this calls __get()
$foo->invader;       // => "Codeignite"

// attempt to get a data[key] that isn't set
$foo->invalid;       // => null
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900