note 87191 added to function.require

From: Date: Sun, 23 Nov 2008 21:37:55 +0000
Subject: note 87191 added to function.require
Groups: php.notes 
Request: Send a blank email to php-notes+get-146848@lists.php.net to get a copy of this message
In response to some dot user at notarealdomain dot com: This is because require executes the code "as if" it was code written inside of the function, inheriting everything including the scope. But here there is something even more interesting: <requiredfile.php>: <?php $this->a.=" is visible also under a require\n"; $b="While the variable b is a local variable of the function\n"; function FunctionUnderRequire() { echo "But the functions declared inside of a require called from a class function, just as when defined from inside any other function, are always global\n"; } ?> <mainfile.php>: <?php error_reporting(E_ALL|E_STRICT); class UserClass { protected $a; public function UserFunction() { $this->a='The class variable a'; require 'requiredfile.php'; echo $this->a; // "The class variable a is visible also under a require\n" echo $this->b; // Notice: Undefined property: UserClass::$b echo $b; // "While the variable b is a local variable of the function\n" $this->FunctionUnderRequire(); //Fatal error! FunctionUnderRequire(); // "But the functions..." } } $UserClass=new UserClass; $UserClass->UserFunction(); ?> I'm wondering if there is a method for declaring class public/private/protected functions from inside a require/include... ---- Server IP: 212.124.37.9 Probable Submitter: 78.53.44.28 ---- Manual Page -- http://www.php.net/manual/en/function.require.php Edit -- https://master.php.net/note/edit/87191 Del: integrated -- https://master.php.net/note/delete/87191/integrated Del: useless -- https://master.php.net/note/delete/87191/useless Del: bad code -- https://master.php.net/note/delete/87191/bad+code Del: spam -- https://master.php.net/note/delete/87191/spam Del: non-english -- https://master.php.net/note/delete/87191/non-english Del: in docs -- https://master.php.net/note/delete/87191/in+docs Del: other reasons-- https://master.php.net/note/delete/87191 Reject -- https://master.php.net/note/reject/87191 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146848) next »