Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > class and method access

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-25-07, 09:49
Zamolxe Zamolxe is offline
Registered User
 
Join Date: Feb 2004
Posts: 35
Post class and method access

hello

if i have class1:
Code:
class class1{ //constructor function class1(){ //bunch of declarations here, and other stuff } //the method i need to acces from outside function clean_text(){ } }


and class2:
Code:
class class2{ function display(){ //code here //i want to access clean_text() here without calling the class1 constructor again } }


in class2 i want to access clean_text() function without doing something like:
Code:
$class1 = new class1; $class1 -> clean_text();
which will trigger the constructor in class1, i want direct access to the function.

i dont want to make class2 extends class1

please share your ideas, and some php manual references also! thanks!
__________________
e-commerce studies
Reply With Quote
  #2 (permalink)  
Old 10-25-07, 09:59
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Firstly my question would be why, and in what circumstances would you want to do this?
Secondly my question would be are you using php version 5+ ?
Thirdly my answer would be :
Code:
static public function clean_text() { // implementation here }

Thus you can call class1::clean_text() directly without initialising a type of class1 object.

Incidently for those interested, in PHP version 4 you can ALWAYS call functions from classes as if they were static. This is because php4 class objects are a bit different. Note that you also don't use the "public" declaration modifier in 4.

Last edited by aschk : 10-25-07 at 10:04.
Reply With Quote
  #3 (permalink)  
Old 10-25-07, 10:07
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
I should also point out for those of you budding young php developers (not that i'm old) you must NOT use any of the internal variables and functions in that class if you're calling the method statically, unless you have initialised a type of that object.

e.g.
Code:
<? class myclass { private $myvar; public function __construct(){ // do constructing stuff $this->myvar = 'a string'; } static public function myStaticFunction(){ return $this->myvar; } } echo myclass::myStaticFunction(); ?>

The above WILL fail.

Last edited by aschk : 10-25-07 at 10:19.
Reply With Quote
  #4 (permalink)  
Old 10-25-07, 10:27
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
Now of course there are ways around the above, one of which is to set an internal private variable as static. Bear in mind that this will be the case throughout ALL instances of myclass that you create.

Code:
<?php class myclass { static private $myvar = "starting string"; public function __construct($newvar=null){ if(!is_null($newvar)){ self::$myvar = $newvar; } } public static function myStaticFunction(){ return self::$myvar; } } echo myclass::myStaticFunction(); echo "<br/>"; $class1 = new myclass(); echo myclass::myStaticFunction(); echo "<br/>"; echo $class1->myStaticFunction(); echo "<br/>"; $class2 = new myclass("a string"); echo myclass::myStaticFunction(); echo "<br/>"; echo $class1->myStaticFunction(); ?>

Try the above and see what you get. You may note, out of interest, that when the $class2 is initialised it sets the internal static variable to "a string", and then when the LAST $class1->myStaticFunction is called it will echo out "a string", instead of what it did the first time which was "starting string", however we're not changed anything inside the $class1 object.

tip: be careful what you do with statics...

Last edited by aschk : 10-25-07 at 10:30.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On