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 383637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:18:08+00:00 2026-05-12T15:18:08+00:00

Wondering if this is possible in PHP Land: Let’s say I have a class

  • 0

Wondering if this is possible in PHP Land:

Let’s say I have a class as follows:

class myClass{
  var $myVar;
    ...
  myMethod(){
      $this->myVar = 10;
  }
}

and another class:

class anotherClass {
   ...
   addFive(){
       $this->myVar += 5;
   }
}

The ‘anotherClass’ is 3500 lines long and I just want the single ‘addFive’ method to use in myClass.

  • Is there a way I can import the function and be able to call it in my class and the $this would reference the myClass object?
  • Is this good/bad practice?
  • (optional) How does this work in Python? (Just curious as I’m starting to learn Python)
  • 1 1 Answer
  • 1 View
  • 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-12T15:18:08+00:00Added an answer on May 12, 2026 at 3:18 pm

    The easiest way to do this is have one class extend the other

    class myClass extends anotherClass {
    }
    

    The myClass class now has access to all the methods of anotherClass that are public or protected.

    If you only want the class to have one method of the other, or it’s not practical to have one class extends from the other, there’s nothing built into PHP that will allow you to do this. The concept you’re looking to Google for is “Mixin”, as in Mix In one class’s functionality with another. There’s an article or two on some patterns you could try to achieve this functionality in PHP, but I’ve never tried it myself.

    Good idea/Bad Idea? Once you have the technique down it’s convenient and useful, but costs you in performance and makes it harder for a newcomer to grok what you’re doing with your code, especially (but not limited to) someone less familiar with OO concepts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no experience with PHP and I am wondering if this is possible:
I'm wondering if this kind of thing will ever be possible in PHP (and
I was wondering if this was possible... I have a sequence of variables that
I was wondering if it would be possible to do this: $var = '$something
Ok, just wondering on the versions of PHP that this class is built into.
Hi I was wondering if this is possible, this is my scenario, I have
i've been coding asp and was wondering if this is possible in php: $data
Wondering if this is even possible or a limitation of PHP, googling around seems
I was wondering if this was possible, and if so how I'd go about
I'm just wondering if this is possible, and if not, the best way to

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.