In PHP, how do you add functions from an include file to an existing class or object instance?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



In PHP, how do you add functions from an include file to an existing class or object instance?



I'm working on a custom PHP framework where the user encapsulates his code for each page in functions. Then those functions will hopefully be added to a general Page class behind the scenes, which determines how to display the page using the functions the user defined. So far I've been unable to add the functions the user defines to the Page class.



I've tried an include statement right in the class, and as expected, that didn't work. I've tried including the include file outside the class and then adding the existing functions to an instantiated object, but I couldn't figure out how to assign a reference to an existing function to a new object property.



Is there a way to take a list of PHP functions from an include file and add those functions either to a class (before object instantiation) or to an object (after instantiation)?



EDIT (MORE INFORMATION AS REQUESTED):



The user will define a page file with functions for each section of code, such as:


function ajax()
function action()
function logic()
function css() <? ?>
function html() <? ?>
function js() <? ?>



We're planning on making these the only code inside each page file and there will be no code outside a PHP function. Then, we're hoping to put these files into a Page class that will display the correct header and footer, inherit from parent classes depending on the section of code it's in, call each of these functions in the correct order and only if appropriate, etc.



We could theoretically keep these functions outside the Page class and just have the page class call them, but the functions would still need to be in a class because we need an efficient way to pass values between the functions. All of our existing code from our old framework stores values using the $this keyword, which we wanted to keep to ease the transition to the new framework. For example


function logic()
$this->userName = $db->getValue("SELECT user_name FROM users WHERE id=" . $_SESSION['user_id']);


function html() <?
<div>Welcome, <?=$this->userName?>!</div>
?>



Ideally we could keep the file as just a list of functions. If there is no way to do that, the next step would be to require every page file to wrap all the functions in its own class and then inject that class into the Page class, but that's not quite as clean as we were hoping to achieve. For example:


class UniqueClassName
function ajax()
function action()
function logic()
function css() <? ?>
function html() <? ?>
function js() <? ?>





This is one of the things in PHP which bother me. Sometimes you just don't want to define an entire class (sometimes BIG) in just 'one file'...
– IncredibleHat
Aug 9 at 17:21





Can you show an example of how the user's function would interact with your class? Are you not able to inject your class into the functions or have the user define a class that extends your class?
– Devon
Aug 9 at 17:24





php.net/manual/en/language.oop5.traits.php
– Felippe Duarte
Aug 9 at 17:29






@Devon added more information as requested.
– dallin
Aug 9 at 17:45





@IncredibleHat, I don't see why you would. Any time you're creating a huge class, you're likely breaking the single responsibility principle. PHP also has the ability to use inheritance and traits to make it so you can easily extend class functionality without growing the size of a single file.
– Devon
Aug 9 at 17:59




2 Answers
2



You can use Traits


trait PageFunctions

public function ajax()
public function action()
public function logic()
public function css() <? ?>
public function html() <? ?>
public function js() <? ?>


class Page
use PageFunctions;


$page = new Page();
$page->js();



If you need to overwrite a trait function, you can


class CustomPage
use PageFunctions;

public function html()
echo "<div>Welcome, <?=$this->userName?>!</div>";



$customPage = new CustomPage();
$customPage->html(); //output <div>Welcome, username!</div>



Your method of having the user write functions without a class would be incredibly dirty for IDEs. Most IDEs would be complaining left and right if you were trying to use $this outside of a class context.


$this



It seems you're making it hard on yourself just to avoid having a user declare a class. From a brief review, it really seems like inheritance would work for this if you really need them to be encapsulated together.



Why not:


class UserPage extends Page
function ajax()
function action()
function logic()



Not only would this give you the ability to instantiate their page and call whatever handlers you have on the Page class, but it also gives them the ability to use pre-defined helpers in your Page class. Any method/property you don't want exposed to UserPage can be set to private.



You could also make page an abstract class forcing the user to properly define these methods.





I agree with your reasoning. I was coming to a lot of the same conclusions as I thought through your first comment as well. I appreciate your time and well thought out answer. I've upvoted your answer but I think I'll wait to accept the answer, as this provides a solution for me personally but does not provide an answer to the original question.
– dallin
Aug 9 at 18:55






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered