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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:17:26+00:00 2026-06-13T04:17:26+00:00

I have a class like this: class sqlClass { var $myvar = test value

  • 0

I have a class like this:

class sqlClass    {
  var $myvar = "test value 1"
  public function test01() {
    global $myvar;
    //some operations here
    $myvar = "test value 2"
    return true;
  }
}

In other file I have this PHP script:

include_once('functions.php'); // where my class is
$data = new sqlClass();
if ($data->test01()) {
  echo $data->myvar;
} else { echo "No value"; }

Well in this example test01() is always true, so I have a guarantee that $myvar changes, BUT, when I print $myvar after I executed the function in the class that is supposed to change the value of it, it prints the old value “test value 1” instead of “test value 2”. So, what am I missing?

  • 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-13T04:17:28+00:00Added an answer on June 13, 2026 at 4:17 am

    Ok so first of all you shouldnt use the var keyword. Thats php4 which you shouldnt be using unless you are maintaining a legacy app. Instead you use one of the visibility keywords – public,protected, or private.

    Secondly you shouldnt use global within a class there shouldnt be a need. to access a class member you use $this->memberName if you need access to variables outside the class you should pass those in as arguments to your methods or constructor.

    So given all that this is what your code should look like:

    class sqlClass    {
      public $myvar = "test value 1";
    
      public function test01() {
    
        //some operations here
        $this->myvar = "test value 2"
    
        return true;
      }
    }
    

    Having said that i would read the entire section on OOP in the manual for php5

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

Sidebar

Related Questions

I have a class like this: class someClass { public static function getBy($method,$value) {
I have a class like this: class A { public $var = ; function
I have a class like this: public ref class Test { public: property int
I have class like this class variable { public: variable(int _type=0) : type(_type), value(NULL),
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a class like this public class foo { private void getThread() {
I have a class like this: public class Contest { List<ContestTeam> Teams { get;
I have a class like this class GUI : public QWidget, public QThread When
I have a class like this, public class person { name Name {set; get;}
I have a class like this: public class MyClass { private Queue<MyOtherClass> myQueue; }

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.