Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2004
    Location
    Bucharest
    Posts
    37

    Post Unanswered: 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!

  2. #2
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    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.

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    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.

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •