Call function in Class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smartic
    New Member
    • May 2007
    • 150

    Call function in Class

    I need to use this function :
    [PHP]mb_internal_enc oding("UTF-8");[/PHP]
    Into class to use it for every mb_ functions into other classes extends from the main class put it give me this error syntax error, unexpected T_STRING, expecting T_FUNCTION

    that is my code example :

    [PHP]class common{
    mb_internal_enc oding("UTF-8");
    }
    class create extends common{
    function call($value){
    if(mb_strlen($v alue) < 4){

    }
    }
    }[/PHP]
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Not sure, try moving it into your constructor.

    Code:
    class common{
        // members go here
        public function common()
        {
            mb_internal_encoding("UTF-8");
        }
    }

    Comment

    • smartic
      New Member
      • May 2007
      • 150

      #3
      Thank you for your reply

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by smartic
        Thank you for your reply
        Have you resolved your issue?

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          despite Markus' solution you may want to have it as a class method.... depending on what you want to do with it.
          note that an extending class with its own constructor doesn't call the parent's constructor.
          [PHP]class common
          {
          // properties go here
          public function encode() { // public, static, final keywords require PHP 5
          mb_internal_enc oding("UTF-8");
          }
          }

          // call:
          $test = new common;
          $test->encode(); // calls the function[/PHP]

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            I overlooked the class extension use. So, adding to Dormi's post: if you were to use a class extension, you could call the parent's constructor like so:

            Code:
            class test
            {
            	# members
            	private $member;
            	#constructor
            	public function test ()
            	{
            		echo 'Hello from <code>test</code>';
            	}
            }
            
            class extension extends test
            {
            	# members
            	private $member;
            	# construct
            	public function extension()
            	{
            		// call to parent constructor
            		parent::test();
            		// or
            		parent::__construct();
            	}
            }
            
            $extension = new extension();
            Teamwork.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              If we don't watch out, this might become a "code slam" (though I'm better in code than in poetry)....

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                If you don't watch out, this might be a Dormilich Slam. If you catch my drift.

                ;)

                Comment

                Working...