Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-03-16 19:44:25

Psilo
Member
From: Iceland
Registered: 2006-04-27
Posts: 11
Website

Can't instantiate php classes defined in external files

Hi,

I am facing some serious problems when trying to instantiate a php class which is defined in an external file, that I include in my plugin code with require_once. I simply get a Fatal error: Cannot instantiate non-existent class.

The file is being included as I can echo output to the screen from within it, but I can’t instantiate classes nor call functions in it from my plugin file, that tries to include it.

To write the classes into my plugin file is out of question, since it is a 3rd party library I am trying to use and consists of several files.

I have got working doing an ugly hack in Textpattern and including it in publish.php, but there must be another way to get this to work without hacks.

Any help on this is highly appreciated. Thanks!


Never make the same mistake twice… there are so many… try a new one each day!

Offline

#2 2007-03-16 22:27:57

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: Can't instantiate php classes defined in external files

Can you show exactly how you’re doing this (the code)?

Offline

#3 2007-03-16 23:27:37

Psilo
Member
From: Iceland
Registered: 2006-04-27
Posts: 11
Website

Re: Can't instantiate php classes defined in external files

Yup!

I am developing in my plugin cache and have a file csm_myplugin.php

The file has the following function:

function csm_myplugin($atts) {
  $lib_file_path = site_url('')."textpattern/ext/mylib/mylib_with_classes.php";
  require_once($lib_file_path);
  $myobj = & new mylib_class();
}

As you can extract from the code above, I have a library with this file mylib_with_classes.php, that contains class definitions, eg. mylib_class(). The file gets included, as I can tell by some plain echo output I do, but the class can not be initiated. See my post above for error message details.

Last edited by Psilo (2007-03-16 23:37:36)


Never make the same mistake twice… there are so many… try a new one each day!

Offline

#4 2007-03-16 23:47:35

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Can't instantiate php classes defined in external files

I think by using site_url, you’re including the mylib_with_classes.php file as parsed by the webserver… so that’s plain HTML (which is why the echo showed something).

Try this:

function csm_myplugin($atts) {
  $lib_file_path = $txpcfg['txpath'] . "/ext/mylib/mylib_with_classes.php";
  require_once($lib_file_path);
  $myobj = & new mylib_class();
}

or

function csm_myplugin($atts) {
  $lib_file_path = txpath . "/ext/mylib/mylib_with_classes.php";
  require_once($lib_file_path);
  $myobj = & new mylib_class();
}

Last edited by ruud (2007-03-16 23:51:16)

Offline

#5 2007-03-16 23:58:23

Psilo
Member
From: Iceland
Registered: 2006-04-27
Posts: 11
Website

Re: Can't instantiate php classes defined in external files

Thanks Ruud, that does the trick ;o) of course need to global $txpcfg; using your first example, the second example works also flawlessly.


Never make the same mistake twice… there are so many… try a new one each day!

Offline

Board footer

Powered by FluxBB