Object Oriented Programming in Macro Engine

    Object Oriented Programming in Macro Engine

    Hi,

    Is it possible to use an object oriented approach using the macro engine?

    Just found out that this code works.

    PHP Source Code

    1. class MyClass
    2. {
    3. function __construct()
    4. {
    5. activesheet()->range("A1")->value = "works";
    6. }
    7. }
    8. function __open()
    9. {
    10. $obj1 = new MyClass;
    11. }



    But this doesn't:


    PHP Source Code

    1. class MyClass
    2. {
    3. public $as = activesheet();
    4. function __construct()
    5. {
    6. $this->as->range("A1")->value = "works";
    7. }
    8. }
    9. function __open()
    10. {
    11. $obj1 = new MyClass;
    12. }



    Is there anything to include or refernce to within the class?

    Post was edited 1 time, last by “Peter Parks” ().

    Hi Madis,

    thanks. It works. But this would give me no real benefit prior to programming in a procedual manner.

    I found a link OOP for php, where building classes is described and so are their benefits. So what i want to achieve is having lots of properties that can be used by various functions.

    I don't really know why the activesheet()-Method for example works within a method/function of MyClass but it doesn't if it is stated in the class' properties?

    Could you maybe clarify your comment on initializing constant variables? Or can you explain why it works within a function of a class but not as a class' property? It would help me a lot.

    Thanks in advance,
    Peter
    Hi Peter,

    Your questin seems to be valid. I checked that there is no limitation of which variable types you can set as class properties in PHP5 so basycally you could could use methods to define class properties. You could try something simple to see if this kind of limitation still exists in macro engine:

    PHP Source Code

    1. class DayClass {
    2. // class properties should not be public, use public methods to access them to makse sure they have been properly initialized just as in this example where $as can not be initialized without constructor
    3. private $as;
    4. private $day=date("l");
    5. public function __construct(){
    6. $this->as = activesheet();
    7. $this->as->range("A1")->value = $this->day;
    8. }
    9. }


    Anyway your plan (private $as) sounds intriguing, I'll try later to figure out some practical use for it :)
    Hi Madis,

    thanks for your hint on setting private properties. I tried your example and set up some others to try if just the activesheet()-method doesn't work. Guess what, every class property which is defined by a method doesn't work. So basically it does exist in the macro engine. Do you for sure know it works in Php 5?

    If so, could the php.ini 's settings be the restricting limitation?

    Greets,
    Peter
    Hi Peter,

    PHP handbook says:
    In PHP 4, only constant initializers for var variables are allowed. (php.net/manual/en/keyword.class.php)
    I recall that one time I was unable to set $var=array(something). I just gave up and used constructor instead. Maybe such feature is not present in macro engine. What version are you using? Macro engine should be based on PHP5, I guess.

    I'm not sure if there's any ini setting you can use (and which ones are supported). Those settings are stored in core/etc/macro_engine.xml, by the way.

    Cheers,
    Madis
    The macro engine uses php5 for sure, just found it in the manual.

    I read the link you posted and I think i kind of get the problem. In the macro_engine.xml I can't identify any options that would match that problem.

    Maybe i can ask you another question if you would have to set up some more complex functions that basically use the same set of variables over and over again, is there a way to define them in order to use them in various functions/methods without oop? I dont want to define them globaly as it seems to be a bad programming habit.

    Greets,
    Peter

    Post was edited 2 times, last by “Peter Parks” ().

    Hi Peter,

    My take on OOP is to make the template content easily manageable so that any changes not affecting layout do not require editing templates.

    Below is an example. Whenever I create new wb, I just add A1=INIT("Template") which calls some php include containing all methods for this particular wb. In this case OOP is not fully leveraged, I am abusing it to make the things more maintainable. All variables, names, formulas &c are easily visible in the code. Too bad that form elements and dynaranges can not be manipulated this way (or I haven't found a way yet). Of course it is possible to write complex methods which can create and manipulate table layouts nicely. But I don't think there's any way to to handle events like "double click on node to expand rows".

    PHP Source Code

    1. // This is the only function in macro_conf.xml. New features can be made available without restarting core server :)
    2. // TODO: figure out how to properly use register_function() so that events can be handled without macros embedded in WS
    3. function INIT($t){
    4. include $t.'.php';
    5. initWB();
    6. }
    7. // Template.php
    8. include 'stuff';
    9. function initWB(){
    10. $sf=new SbstFactory();
    11. $nf= new NameFactory();
    12. $ff=new FunFactory();
    13. // Sheet
    14. $myDim=$ff->getParam('myDim');
    15. $dimList=$sf->getDimSbst($myDim); // $sf method body containt this: '=PALO.SUBSET(appconn,"'.$ff->getParam($myDim).'",1,,,,,,,PALO.SORT(0,0,,0,,0,1))';
    16. // TODO: implement DataFactory
    17. $elName='=PALO.DATAC(appconn,"#_"&"'.$ff->getParam($myDim).'",'Text',C8)';
    18. $sf->setVersionSelector();
    19. $nf->setName('dimList', $dimList);
    20. $nf->setRange('Sheet1', 'E8', $elName);
    21. }
    22. // NameFactory.php - for example
    23. class NameFactory {
    24. private $b;
    25. public function __construct() {
    26. $this->b=activeworkbook();
    27. }
    28. public function setName($range,$refersto) {
    29. $r=$this->b->names->item($range)->refers_to=$refersto;
    30. return $r;
    31. }
    32. public function setRange($sheet,$range,$formula) {
    33. $r=$this->b->worksheets()->item($sheet)->range($range)->formula($formula);
    34. return $r;
    35. }
    36. }


    Cheers,
    Madis
    Hi Madis,

    this approach is well thought and easy to understand and also to replicate. I was wondering how to manage my code properyl, but with this example I should be able to do so - Thanks a lot.

    I understand your problem managing your code from within the sheet itself but this is just because you have so limited options to call macros from within the workbook.

    Once more thanks for your input.

    Greets,
    Peter

    Post was edited 1 time, last by “Peter Parks” ().