Page 1 of 1

"new" keyword/construction in UDTs

Posted: Mon Dec 08, 2008 7:20 pm
by andrewwyld
Hi there,

I am a bit new to PHP and CMS Made Simple, so this is perhaps a newbie question.  Appropriately enough, it concerns new and constructors.  My background is in C and C++ so it's quite likely I am making assumptions about how they work that are wrong.

I'm working on a UDT in CMS Made Simple 1.4.1 on PHP 5.1.6.  I have built a very simple XML file for use in a site; basically it contains contact details for various site offices, and each one has an ID which corresponds to a site id field in a SQL database.  I plan to grab the id, I use it to select the appropriate bit of the xml and I print the bits into a table.

Since the version of PHP I have is over five, I thought I would use the DOM, and found a nice (so I suppose) tute here:

http://devzone.zend.com/node/view/id/1713#Heading8

It suggests loading your XML using

$dom = new DomDocument();
$dom->load("articles.xml");

I have stuck something very similar into my UDT (identical barring variable and file names, basically) and I have discovered that it stops working around the point of the first statement (the construction statement).  Moving it up the file kills everything after it, and that goes with everything commented out but the construction statement, so it's definitely the actual construction that is causing the grief.  It seems to parse fine.  I also found that the class is actually DOMDocument so I changed the capitalization; no effect.

Anyway, I had had problems with global variables earlier (I had to specify they were global within the UDT, which I didn't think you had to do if you declared them outside a function) so I wondered if UDTs mess around with memory management in some way?  Alternatively, do I have to tell the UDT to use the DOM in a sort of #include kind of a way?  Is it something compeltely different?

Thanks!

Re: "new" keyword/construction in UDTs

Posted: Mon Dec 08, 2008 8:21 pm
by calguy1000
Nope, UDT's are essentially PHP functions that are stored in the database.  The problem with globals that you had was not UDT related, it's a PHP inexperience thing.

If you're experiencing problems like this check your error log.

Re: "new" keyword/construction in UDTs

Posted: Mon Dec 08, 2008 10:06 pm
by andrewwyld
Re globals, that was what I thought it might be—from what I read variables act global if you declare them at global scope, so if you declare them at function scope (like in a UDT) you'd need to specify they were global.  I am slightly surprised it let me define a function in the UDT if the UDT was itself a function, but I am not complaining :)

I have no access to the log, unfortunately.  What logs I can find are yesterday's.  However, I did try

if(class_exists("DOMDocument")){
  print "CHING CHING!";
}
else{
  print "no beans for you";
}

and there were no beans for me.  Is there any reason why DOMDocument would be missing, or why class_exists("...") might return a false negative?

Re: "new" keyword/construction in UDTs

Posted: Mon Dec 08, 2008 11:09 pm
by calguy1000
that'd be a PHP config issue.....

do a phpinfo(); see what is returned

the DOM stuff should be in there somewhere.
if it isn't (and I'll bet it isn't)... contact your host.

Re: "new" keyword/construction in UDTs

Posted: Tue Dec 09, 2008 12:10 am
by andrewwyld
Aha!  It was apparently built with --disable-dom.  God knows why.  Thanks!

Re: "new" keyword/construction in UDTs

Posted: Tue Dec 09, 2008 12:15 am
by calguy1000
and btw... php allows for nested functions.

Code: Select all

<?php
function pythag($a,$b)
{
  function square($n)
  {
     return $n * $n;
  }

  return sqrt(square($a)+square($b));
}

echo "pythag of 5 and 4 is ".pythag(5,4)."<br/>";
?>