1
nyman
[Solved] Objetc method use in templage problem
  • 2005/3/10 16:50

  • nyman

  • Just popping in

  • Posts: 11

  • Since: 2004/1/9 1


Hi, I'm writing a new module and I've a problem:

This is the context:

Have a defined class:

class myClass{
var $name;
function myClass
{
$this->name="This is the name";
}
function getName()
{
echo $this->name;
}
}

ok, in my index.php :

...
includes ...
...

myObject = new myClass();

...

...

$tplXoops->assign( 'myObject', $myObject );

include XOOPS_ROOT_PATH."/footer.php";
?>


and last, in template ...

This is the value: <{$object->getName()}>


Well, if I write in template :

This is the value: <{$object->name}>

it works perfectly ... but when I try to call a method function I get a Non-object call function etc etc
when compiling template ... but this is not all ... I get the error, but module and template works ...


Why do I get that error when updating module ??
Any one has a possible response ?

Thanx in advance !

2
Mithrandir
Re: Objetc method use in templage problem

When updating the module, the template is fetched by a Smarty function that has the purpose of putting it in the templates_c directory (weaving a bit, not entirely certain of all the details in this area)
Since the template is "run" at a time where there are no objects loaded in memory (as the corresponding PHP file is not run) it will not have the objects to call the methods on. This is not a big problem with arrays as the worst thing that can happen is a notice about an undefined index - but calling a class method on a non-object is a fatal PHP error.

A workaround (that I don't know the consequences of, but I have not seen any negative ones, yet) is to enclose the code in the template, where you use the object, with
<{if $object}>
blah blah blah
<{/if}>
so it will not be executed when the $object variable is not set (as in admin, when updating the module)

3
nyman
Re: Objetc method use in templage problem
  • 2005/3/10 17:28

  • nyman

  • Just popping in

  • Posts: 11

  • Since: 2004/1/9 1


Workaruond works.

What I don't understand is that $object->propertie is known ( so class definition is known, isn't it ) and $object->method() is not

Any way, as I've said, workaround works. Thank you very much !!

4
Mithrandir
Re: Objetc method use in templage problem

$object->property on a non-object does not cause a fatal error (if I remember correctly) whereas $object->method() does.

5
nyman
Re: Objetc method use in templage problem
  • 2005/3/10 17:37

  • nyman

  • Just popping in

  • Posts: 11

  • Since: 2004/1/9 1


You are the One.
Tnx.

Login

Who's Online

447 user(s) are online (317 user(s) are browsing Support Forums)


Members: 0


Guests: 447


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits