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

  • SEARCH
  • Home
  • 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 8164029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:12:50+00:00 2026-06-06T19:12:50+00:00

In PHP, is it possible to have a function within a class that’s non-static,

  • 0

In PHP, is it possible to have a function within a class that’s non-static, but also isn’t an instance function?

For example, if I have the following:

class A
{
   public $i;

   function setValue($val) {
      $this->i = $val;
   }
}  

$a1 = new A;
$a1->setValue(5);
echo $a1->i; // result: 5

$a2 = new A;
$a2->setValue(2);
echo $a2->i; // result: 2

Can I add a function to that class that can have “visibility” of all instances of itself so I can do something like so (which I know doesn’t work, but communicates my thought):

class A
{
   public $i;

   function setValue($val) {
      $this->i = $val;
   }

   function getTotal() {
      return sum($this->i); // I know sum() isn't a built-in function, but it helps explain what I want. I'm not sure if $this makes sense here too.
   }
}  

$a1 = new A;
$a1->setValue(5);
echo $a1->i; // result: 5

$a2 = new A;
$a2->setValue(2);
echo $a2->i; // result: 2

echo A::getTotal(); // returns: 7

I guess A::getTotal() means getTotal() would need to be static, but if it was static then it wouldn’t then be able to “see” each class instance.

Is this type of thing possible, and what’s the correct terminology I should be using?

  • 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-06-06T19:12:51+00:00Added an answer on June 6, 2026 at 7:12 pm

    No, there is no built-in instance enumeration, you will need to keep references to each instantiated object yourself. You can keep an array of instances in a static property of the class and populate it in your __construct(). You can then have a static method loop over this array and process all instances.

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

Sidebar

Related Questions

Possible Duplicate: How to use include within a function? i have two file inc.php
Is it possible to have a PHP function that is both recursive and anonymous?
Is it possible to call a PHP function within an anchor tag. I have
I believe it is possible to execute a java script function within PHP, but
Is it possible to have JavaScript compute a timestamp returned from PHP's time() function
Possible Duplicate: php false place in condition I have noticed that a lot of
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
I want to have horizontal lists that can run as wide as possible but
Possible Duplicate: How to access a variable in a PHP class that is set
Possible Duplicate: php: determine where function was called from I have an autoloading function

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.