1

I create a PHP page with the following content:

<?php session_start(); ?>
<?php require_once(./classes/MaterialUtil.class.php);
$mUtil = new MaterialUtil(); 
?>

I put the MaterialUtil.class.php file in D:\xampp\htdocs\drupal\sites\all\themes\zeropoint\classes, but I get the following error message:

Parse error: syntax error, unexpected '.' in D:\xampp\htdocs\drupal\modules\php\php.module(80) : eval()'d code on line 7

Could you please tell me what I do wrong?

apaderno
  • 28,547
  • 16
  • 75
  • 90
user6919
  • 121
  • 1
  • 6
  • 2
    First of all, you are missing php syntax. You have to wrap path with quotes: `require_once('./classes/MaterialUtil.class.php')`. The second thing is that your code is not for Drupal. Maybe you should start with articles about module development in Drupal. – kalabro Apr 20 '12 at 07:59
  • I put MaterialUtil.class.php in D:\xampp\htdocs\drupal\sites\all\themes\zeropoint\classes Is it right? If incorrect, where should you put? –  Apr 20 '12 at 08:06
  • Unfortunately I can't see what are you doing and where is Drupal usage in your example. If you don't bootstrap Drupal, you just have to set full path to file `/sites/all/themes/zeropoint/classes/MaterialUtil.class.php` – kalabro Apr 20 '12 at 08:17
  • I just want to know how to use require_one in Drupal. Could you help me? –  Apr 20 '12 at 08:23
  • Drupal isn't really fancy about OOP and classes. From the path you are providing it seems you are about to create/edit a theme. As kalabro already stated, please read theme development section on drupal.org first. – Paul Apr 20 '12 at 08:37
  • 2
    @Paul Including a class library usually isn't needed in a theme. To me, it sounds as if user6919 is trying to build functionality into a theme while he actually should be [creating a custom module](http://drupal.org/node/361112) and use [module_load_include()](http://api.drupal.org/api/drupal/includes!module.inc/function/module_load_include/7) to include the file. – marcvangend Apr 20 '12 at 09:59
  • As suggested by @marcvangend make use of module_load_include() although I suggest you dig into Drupal Development first, else you will end up with un maintainable piece of Drupal + PHP code – GoodSp33d Apr 21 '12 at 14:06
  • @marcvangend You are right. But the question itself is not that clear to that point. If new functions shall be added, I'd recommend a custom module as well. – Paul Apr 23 '12 at 12:06

2 Answers2

2

The error is caused from the fact you didn't use a string for the filename, and PHP understood the dot as being the concatenation operator; as such, because there wasn't any value before the operator, PHP gave you an error saying it found the concatenation operator in the wrong place.

As kalabro said, the correct code is the following one:

<?php session_start(); ?>
<?php require_once('./classes/MaterialUtil.class.php');
$mUtil = new MaterialUtil(); 
?>

This is the part of the answer that is not strictly related to Drupal.

What you are doing is not what I would suggest to do, for two reasons:

  • You are putting the "classes" directory in the wrong place. Those files are not related to the theme being enabled, but they are related to the page being viewed. Even if you have a single theme, and users are not allowed to select a theme for themselves, it still wrong to put those files in a theme directory.
    Putting the files in the directory containing a theme, which will needs to be updated when a new version is available, could cause you to lose the additional files you added, if you are not careful.
  • Executing PHP through eval() to, e.g., get content to show in a node is not something that you should do. This is because:
    • As you have used the PHP filter for the node, the node becomes only editable to a restricted group of users. (I would not suggest to allow untrusted users to use PHP as input format)
    • When you have PHP code that you need to execute, it is always better to create a custom module that is enabled for the site.

If you were trying to include a PHP file from inside a module, then you should use module_load_include(), instead of require_once(), as already suggested by marcvangend.

apaderno
  • 28,547
  • 16
  • 75
  • 90
0

XAMP server does not run on windows file system format. You must write your file location like localhost/xyz/abc ..

hakki
  • 6,181
  • 6
  • 62
  • 106