Jump to content

Inject a variable constructor into a class function from include file?


kelon

Recommended Posts

Hi

 

Alert! Numpty question.

 

Edit: having written this out I find my question is - is this a scope issue, or should I look into the order my include file is loading, or is it something else? A pointer would be really kind.

 

-----------

 

I've a script written by someone else I'm trying to customise via creating an extra include file. I've tried searching but don't really know what terms to search on. Hope someone will kindly point me in the right direction.

 

In the core script there is a class with a function defining variables:

class PageStuff {
  static function create_vars($page) {
     $page->var1 = something....
     $page->var2 = somethingElse...
     etc., var3-10...
  }
 static function create($page) {
    ....}
}

class Page {
PageStuff::create($this);
}

All I'm trying to achieve is: I want to add another variable constructor to the function in the class such as...

$page->myVar = someCustomVar;

But... from an include file so my customisations are kept separate. If I edit stick it in the core code it works perfectly. However, the moment I try to put it in an include, well, I'm all at sea. Two of my feeble attempts at something in an include file...

PageStuff::create_vars($page) {
  $page->myVar = someCustomVar;
}

PageStuff->$page['myVar'] = someCustomVar;

The error response is: "....class PageStuff not found..." If I print_r(get_defined_vars()) from within my include file... there's no evidence of either class (PageStuff or Page) and var_dump($page) outputs NULL.

 

If you know a good, simple, resource for customising scripts via include files, that'd be great. I've not been able to find anything! But then, I'm struggling with whether I should be searching for plugin, hook or...well something else.

 

This feels like I'm trying to swallow an elephant :-) Thank you for any encouragement you might charitably give and thank you for reading regardless.

Link to comment
Share on other sites

If $page is a dynamic variable it can only be called on an instance of PageStuff. e.g

$pageStuff = new PageStuff();
$pageStuff->page = $customVar;

In your case, the $page object is being passed to the create_vars() method, which can modify the object but after the method completes, the $page object goes out of scope.

Link to comment
Share on other sites

Hi Syed

 

Thank you for responding. Apologies for the late acknowledgement - been working.

 

Yes, in the bit I'd continued to do on this to try to figure it out for myself, I got as far as working that out, though it's nice to have it confirmed, thank you as I wasn't 100% sure I was understanding it correctly.

 

I'm currently exploring the idea of sticking hooks into the method create_vars(). It means altering the core code, but I've decided that if I document the hooks properly in myhooks.inc.php, then I should in future be able to figure out what was what should the core code be updated.

 

Thanks again.

Link to comment
Share on other sites

Just for anyone else who happens in here on a similar mission, I've found this resource (note: written in 2008) about plug-ins, explained well http://knowledgebox.blogspot.co.uk/2008/06/do-it-yourselft-plugins.html

 

Most resource seems to be about Wordpress/Drupal or some other... seems very little about the basics like this.

 

I was incorrect above to say "it means altering the core code"... this isn't what I want to do and plugins do not work that way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.