Jump to content


Scoping Rules For Globals And Classes In Snippets


4 replies to this topic

#1 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 15 April 2006 - 12:12 PM

I want to be able to break some of my larger (unpublished) snippets down into more manageable units without having lots of duplicated database accesses. To do this I'd need to share quite a lot of data between the smaller snippets.

Before I start down this track, a couple of questions for the more knowledgeable developers here:

1) If I declare a variable in a snippet as global, can I access and modify it from another snippet. If so where do I need to declare it global. (I think i've finally got to grips with scoping rules related to variables and functions - this is one area where a good knowledge of 'c' and 'c++' does not help with PHP!)

2) If I define a class and its methods and variables in one snippet, and create an instance of the class, can I access the instance, methods and global and static variables from another snippet, or do I need to add them to index.php? [In practice, I'd probably add them to index.php for my own use, and release as a snippet for others.]

TIA
mike

Edited by mikef, 15 April 2006 - 12:14 PM.


#2 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 15 April 2006 - 02:41 PM

Hey, mikef, I'm already a step ahead of you on that... As several members already know, I have already started coding using modular concepts for several of the projects I have been working on... Here is an overview of how it works... The module has a control snippet... That snippet handles all module-specific variable handling and passes that data to each sub-snippet as an array which is then extract()ed at that child snippet... And, yes, I have a tutorial inthe works... An example of this technique would be Deans etoCatalog... This technique works well with any project that does any amount of data management and/or does a lot of conditional processing... The form class that comes packaged in Etomite Prelude Final also uses several of the techinques you referred to in your post... :eto:

#3 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 15 April 2006 - 03:40 PM

Ralph (rad14701), on Apr 15 2006, 03:41 PM, said:

Hey, mikef, I'm already a step ahead of you on that... As several members already know, I have already started coding using modular concepts for several of the projects I have been working on... Here is an overview of how it works... The module has a control snippet... That snippet handles all module-specific variable handling and passes that data to each sub-snippet as an array which is then extract()ed at that child snippet... And, yes, I have a tutorial inthe works... An example of this technique would be Deans etoCatalog... This technique works well with any project that does any amount of data management and/or does a lot of conditional processing... The form class that comes packaged in Etomite Prelude Final also uses several of the techinques you referred to in your post... :eto:
but the form class isn't in a snippet, its directly included in index.php! I know it'll work if its in index.php, otherwise the etomite class wouldn't work.

Edited by mikef, 15 April 2006 - 03:41 PM.


#4 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,524 posts
  • Gender:Male

Posted 15 April 2006 - 04:50 PM

mikef, on Apr 15 2006, 12:40 PM, said:

but the form class isn't in a snippet, its directly included in index.php! I know it'll work if its in index.php, otherwise the etomite class wouldn't work.
Okay... Regarding your question about classes, you can place a class in a snippet and then place the snippet call in your site template, essentially making the class available across the entire site... Simply put a single statement in the snippet that instantiates the class, making it active...

Example:

// myClass Snippet -- placed at the top of your site template
// Usage: [[myClass]]

class myClass {
  function myClass() {
    $myVar1 = "data1";
    $myVar2 = "data2";
  }
  function showVars($var1,$var2) {
    return "Variable One is ".$var1." and Variable Two is ".$var2.".";
  }
}

$myClass = new myClass;

// otherSnippet -- placed in the template or in any document, snippet, or chunk
return $myClass->showVars("Etomite CMS","Prelude Final");


#5 mikef

    Loves Etomite Forums!

  • Member
  • PipPipPipPip
  • 1,551 posts

Posted 15 April 2006 - 05:12 PM

:) Thanks!

Now I just need to find the time to do the restructuring ... documentation first!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users