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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:22:38+00:00 2026-05-15T23:22:38+00:00

scope issue in PHP classes: Why does this work? class index extends Application {

  • 0

scope issue in PHP classes:

Why does this work?

 class index extends Application
  {
        function ShowPage()
        {
            $smarty = new Smarty();         // construct class
            $smarty->assign('name', 'Ned');     // then call a method of class
            $smarty->display('index.tpl');
        }   

}

$index_instance = new index;
$index_instance->ShowPage();

but this does not work?

class index extends Application
{

    function ShowPage()
    {

        $smarty->assign('name', 'Ned');
        $smarty->display('index.tpl');
    }   
}

$index_instance = new index;
$smarty = new Smarty();
$index_instance->ShowPage();
  • 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-15T23:22:39+00:00Added an answer on May 15, 2026 at 11:22 pm

    Welcome to the wonderful world of PHP variable scoping.

    Functions and methods don’t see any variables defined outside of them. You have to use the global keyword to declare that you want access to variables defined outside of the function scope.

    This won’t work:

    class Foo {
        public function bar() {
            echo $baz;
        }
    }
    $f = new Foo();
    $baz = 'Hello world!';
    $f->bar(); // "Notice: Undefined variable: ..."
    

    This will work:

    class Foo2 {
        public function bar() {
            global $baz; // <- "I want $baz from the global scope"
            echo $baz;
        }
    }
    $f = new Foo2();
    $baz = 'Hello world!';
    $f->bar(); // "Hello world!"
    

    Even though it works, you should avoid using it. There are better ways to go about passing in external object. One way is called “dependency injection“, which is a fancy way of saying “pass in external dependencies during construction.” For example:

    class Index extends Application {
        private $smarty;
        public function __construct(Smarty $smarty) {
            $this->smarty = $smarty;
        }
        public function showPage() {
            $smarty->assign('foo', 'bar');
            $smarty->display('index.tpl');
        }
    }
    $sm = new Smarty(...);
    $whatever = new Index($sm);
    $whatever->showPage();
    

    Another way is using a registry, which is a pattern used to store things that otherwise might be global variables. Let’s try out Zend Registry as an example.

    class Index extends Application {
        public function showPage() {
            $smarty = Zend_Registry::get('smarty');
            $smarty->assign('foo', 'bar');
            $smarty->display('index.tpl');
        }
    }
    $sm = new Smarty(...);
    Zend_Registry::set('smarty', $sm);
    $whatever = new Index();
    $whatever->showPage();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does the Function Scope in JavaScript work?
Here is my search function : class Ads extends Controller { public function search($format='velos',
I'm new to the scope of 'this' in html, javascript. So after looking at
Named function parameters can be emulated in PHP if I write functions like this
So this null pointer is confusing me. I believe it is a scope issue.
Scope: PHP-mysql based site, using memcached. Caching issue is mostly about prices. Because of
I'm using the new PHP SDK 3.0 and I use the new getLoginUrl() function
I'm rather new to PHP and I am kind of stuck here writing this
I have an issue that (I think) might have to do with scope, but
Global scope allows you to use a variable in a function that was defined

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.