Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 507429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:48:13+00:00 2026-05-13T06:48:13+00:00

I want to make a PHP class, lets say Myclass.php. Now inside that class

  • 0

I want to make a PHP class, lets say Myclass.php. Now inside that class I want to define just the class itself and some instance variables. But all the methods must come from a Myclass_methods.php file. Can I just include that file into the class body?

I have good reasons why I want to seperate this. In short, I’ll have a backend in which I can change the business logic of a class, while all other things must remain untouched. The system maintains all the ORM and other stuff for me.

But if this is a bad idea, it might be better to re-generate the whole class file after editing the business logic (so, the user-defined methods in this case).

Performance question: If during one request Myclass.php is included just once, actually that Myclass_methods.php should also be included just once. Might be wrong. Experts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:48:13+00:00Added an answer on May 13, 2026 at 6:48 am

    No. You cannot include files in the class body.
    In a file defining a class, you may only include files in a method body or outside the class body.

    From your description I take you want this:

    <?php // MyClass.php
    class MyClass
    {
        protected $_prop;
        include 'myclass-methods.php';
    }
    
    <?php // myclass-methods.php
    public function myMethod()
    {
       $this->$_prop = 1;
    }
    

    Running this code will result in

    Parse error: syntax error, unexpected T_INCLUDE, expecting T_FUNCTION
    

    What is possible though is this

    <?php // MyClass.php
    class MyClass
    {
        protected $_prop;
        public function __construct() // or any other method
        {
            include 'some-functions.php';
            foo($b); // echoes 'a';
        }
    }
    
    <?php // some-functions.php
    $b = 'a';
    function foo($str)
    {
       echo $str;
    }
    

    Doing it this way, will import the contents of the include file into the method scope, not the class scope. You may include functions and variables in the include file, but not methods. You could but should not put entire scripts into it as well and change what the method does, e.g.

    <?php // MyClass.php
        // ...
        public function __construct($someCondition)
        {
            // No No Code here
            include ($someCondition === 'whatever') ? 'whatever.php' : 'default.php';
        }
        // ...
    
    <?php // whatever.php
        echo 'whatever';
    
    <?php // default.php
        echo 'foo';
    

    However, patching the class this way to exhibit different behavior is not how you should do it in OOP. It’s just plain wrong and should make your eyes bleed.

    Since you want to dynamically change behavior, extending the class is also not a good option (see below why). What you really will want to do is write an interface and make your class use objects implementing this interface, thus making sure the appropriate methods are available. This is called a Strategy Pattern and works like this:

    <?php // Meowing.php 
    interface Meowing
    {
        public function meow();
    }
    

    Now you got the contract that all Meowing Behaviors must obey, namely having a meow method. Next define a Meowing Behavior:

    <?php // RegularMeow.php
    class RegularMeow implements Meowing
    {
        public function meow()
        {
            return 'meow';
        }
    }
    

    Now to use it, use:

    <?php // Cat.php
    class Cat
    {
        protected $_meowing;
    
        public function setMeowing(Meowing $meowing)
        {
            $this->_meowing = $meowing;
        }
    
        public function meow()
        {
            $this->_meowing->meow()
        }
    }
    

    By adding the Meowing TypeHint to setMeowing, you make sure that the passed param implements the Meowing interface. Let’s define another Meowing Behavior:

    <?php // LolkatMeow.php
    class LolkatMeow implements Meowing
    {
        public function meow()
        {
            return 'lolz xD';
        }
    }
    

    Now, you can easily interchange behaviors like this:

    <?php
    require_once 'Meowing.php';
    require_once 'RegularMeow.php';
    require_once 'LolkatMeow.php';
    require_once 'Cat.php';
    
    $cat = new Cat;
    $cat->setMeowing(new RegularMeow);
    echo $cat->meow; // outputs 'meow';
    // now to change the behavior
    $cat->setMeowing(new LolkatMeow);
    echo $cat->meow; // outputs 'lolz xD';
    

    While you also could have solved the above with inheritance by defining an abstract BaseCat and meow method and then deriving concrete RegularCat and Lolkat classes from that, you have to consider what you want to achieve. If your cats will never change the way they meow, go ahead and use inheritance, but if your RegularCat and Lolkat is supposed to be able to do arbitrary meows, then use the Strategy pattern.

    For more design patterns in PHP, check these resources:

    • http://www.php.net/manual/en/language.oop5.patterns.php
    • http://www.ibm.com/developerworks/library/os-php-designptrns/
    • http://www.fluffycat.com/PHP-Design-Patterns/
    • http://sourcemaking.com/design_patterns
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to make a php app that let people submit photos/videos/sounds Now, everything
here is myproblem, i want to make a php function to insert some random
I want to make a button that starts my php script after I click
Im doing a database class in PHP and I want to make cache of
i use a php class to make tag cloud from article, but i want
I want to make variables and methods in php class. And want to use
I want to make my php page only accessible from another page redirect and
<?php while(true){ //code goes here..... } ?> I want to make a PHP web
I want to make a little php poll. The script should ask the users
I want to make an array in PHP but it should be in specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.