PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 3rd, 2007, 07:42 PM
topgear24 topgear24 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Location: toronto
Posts: 11 topgear24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 21 m 10 sec
Reputation Power: 0
PHP5 - Including php script within class function

This isn't so much a question of why something doesn't work, but rather a question of why it does work. Using PHP you can include scripts from within class functions and access the included functions globally. This surprises me, it seems to go against what I feel should occur. If someone has a sound reason why this occurs, I would appreciate it.

temp_include.php:
PHP Code:
<?php

function test_funct() {
    echo 
"function called";
}

?>


main script:
PHP Code:
<?php

Class ABC {
    public function 
__construct() {
        require_once 
'temp_include.php';
    }
}

$o = new ABC();

test_funct();

?>

Reply With Quote
  #2  
Old September 3rd, 2007, 07:59 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,548 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 h 59 m 56 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
maybe you can, but the concept is incorrect.. meaning it was not created to work this way.. and things that have not been created to work away... leads to vulnerabilities sometimes.

Reply With Quote
  #3  
Old September 3rd, 2007, 08:00 PM
requinix's Avatar
requinix requinix is online now
Contributing User
Click here for more information.
 
Join Date: Mar 2007
Posts: 3,064 requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)requinix User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 29806 Folding Title: Starter FolderFolding Points: 29806 Folding Title: Starter Folder
Time spent in forums: 2 Months 3 Weeks 5 Days 6 h 58 m 46 sec
Reputation Power: 623
http://us2.php.net/manual/en/language.functions.php
Quote:
Originally Posted by PHP Manual
All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa.

All you're doing is defining a function inside a function: the fact that it's inside a class too doesn't matter.
__________________
If you think I'm being mean, you're probably right.
DEVSHED NOW HAS A REGEX FORUM
"Philosophers, theologians, and all the others who scrutinize supernatural affairs or invisible things all speak a lot of nonsense; for men are, in fact, in the dark about such affairs, and their investigation has served and still serves more to exercise their wits than to discover the truth."

Reply With Quote
  #4  
Old September 3rd, 2007, 08:06 PM
topgear24 topgear24 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Location: toronto
Posts: 11 topgear24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 21 m 10 sec
Reputation Power: 0
Yeah, it seems to be somewhat common practice to include this way anyways. I took a look at the source for Smarty and their Smarty_compiler.class.php includes plugins and other files from functions of that class this way.

Reply With Quote
  #5  
Old September 3rd, 2007, 08:30 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Dec 2004
Posts: 1,108 E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 6 Days 15 h 33 m 15 sec
Reputation Power: 196
Quote:
maybe you can, but the concept is incorrect.. meaning it was not created to work this way.. and things that have not been created to work away... leads to vulnerabilities sometimes.

According to the manual it was created to work that way. If it didn't work that way it would be possible to dynamically add functions to an object; I don't know for sure but I'm pretty sure that doing something like that would requires pretty extensive design changes to the zend engine. Not that it would be a bad thing (javascript supports it and imo javascript's class system is one of the best with the exception of a few small parts of it), but I don't think PHP even could support something like that in its current state.

Reply With Quote
  #6  
Old September 3rd, 2007, 08:34 PM
chadsmith729's Avatar
chadsmith729 chadsmith729 is offline
--Retired--
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Sep 2003
Location: Enjoying forum retirement.
Posts: 3,113 chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)  Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 16 h 46 m 22 sec
Reputation Power: 925
Ok, off topic question is now: Do you really need to use a class for this or are you just curious?

Chad
__________________
Get A Supplier LLC | Hilarious Comic

--- Pretty much retired from the forum. I will get back on every now and then, and maybe answer a few questions.

Reply With Quote
  #7  
Old September 3rd, 2007, 08:43 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Dec 2004
Posts: 1,108 E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 6 Days 15 h 33 m 15 sec
Reputation Power: 196
You don't need to use a class for this. The OP was commenting it was was odd that when you include a file within the scope of a class the functions in the included file are added to the global scope rather than the class scope (as they would if the methods had been hard coded into the class).

Reply With Quote
  #8  
Old September 3rd, 2007, 08:53 PM
topgear24 topgear24 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Location: toronto
Posts: 11 topgear24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 21 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by chadsmith729
Ok, off topic question is now: Do you really need to use a class for this or are you just curious?

Chad


For reasons I won't get into, I'm considering of setting up a plugin system into my application. So yeah, this wasn't just something that popped in my head while watching the Jays getting killed.

Reply With Quote
  #9  
Old September 3rd, 2007, 09:04 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Dec 2004
Posts: 1,108 E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)E-Oreo User rank is First Lieutenant (10000 - 20000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 6 Days 15 h 33 m 15 sec
Reputation Power: 196
Quote:
You don't need to use a class for this. The OP was commenting it was was odd that when you include a file within the scope of a class the functions in the included file are added to the global scope rather than the class scope (as they would if the methods had been hard coded into the class).


Sorry I misunderstood chadsmith's question, I thought he was asking if you needed a class to use include ><

Reply With Quote
  #10  
Old September 3rd, 2007, 09:19 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,548 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1Folding Points: 109960 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 1 h 59 m 56 sec
Reputation Power: 378
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
Quote:
Originally Posted by E-Oreo
According to the manual it was created to work that way. If it didn't work that way it would be possible to dynamically add functions to an object; I don't know for sure but I'm pretty sure that doing something like that would requires pretty extensive design changes to the zend engine. Not that it would be a bad thing (javascript supports it and imo javascript's class system is one of the best with the exception of a few small parts of it), but I don't think PHP even could support something like that in its current state.
hmm like I said before.. you probably can (as it looks like you can) but.. It does not sound like something I would prefer doing.. in other words.. I my self would not trust calling a function in that manner.. now using $var -> classconstfunc this way I would.

Reply With Quote
  #11  
Old September 4th, 2007, 06:53 AM
chadsmith729's Avatar
chadsmith729 chadsmith729 is offline
--Retired--
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Sep 2003
Location: Enjoying forum retirement.
Posts: 3,113 chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)chadsmith729 User rank is General (90000 - 100000 Reputation Level)  Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1Folding Points: 285523 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 16 h 46 m 22 sec
Reputation Power: 925
Topgear:

Ok that's fine, I just had to question that. Because maybe there was another way that you could be doing the project that might be easier and better to maintain.

Great, have a good time and hope you find an answer.
Chad

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP5 - Including php script within class function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off