Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Instantiating a class

    I created a class in my file 'functions.php'. In 'index.php' I include functions and later in the file I have this code;
    Code:
    		Case 'mailit':
    			$thismsg=new mailme;
    			$mailme->setTo($_POST['toadd']);
    			$mailme->setFrom($_POST['fromadd']);
    			$mailme->setText($_POST['mailmsg']);
    			$mailme->setSubject($_POST['mailsub']);
    			$mailme->sendMail;
    			include("mailsent.php");
    			break;
    Im getting this error:
    Fatal error: Call to a member function on a non-object in c:\phpdev5\www\alaskaerrands\index.php on line 131

    My class which is borrowed from several sources, looks like this

    Code:
    class mailme{
    
    	//---Global Variables
    	var $mailTo= "";						var $mailFrom= "";						var $mailSubject= "";						var $mailText= "";					
    
    	function setTo($inAddress){
    		
    		$this->mailTo = $inAddress;
    		return true;
    	}
    I removed the other functions for clarity.

    I just read an article on using classes and I gather I am doing it right, but alas, there is an error so...

    Thanks.
    Scott Stewart
    Always happy to learn from pros.

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    Case 'mailit':
                
    $thismsg=new mailme;
                
    $mailme->setTo($_POST['toadd']);
                
    $mailme->setFrom($_POST['fromadd']);
                
    $mailme->setText($_POST['mailmsg']);
                
    $mailme->setSubject($_POST['mailsub']);
                
    $mailme->sendMail;
                include(
    "mailsent.php");
                break; 
    The variable with the class is $thismsg not $mailme

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts
    How utterly stupid of me! It was so obvious I'm surprised I didn't get whacked up side my head!
    Scott Stewart
    Always happy to learn from pros.

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Posts
    567
    Thanks
    2
    Thanked 4 Times in 4 Posts
    How do I call a function within a class. In my case what is the syntax for calling the sendMail function? The way I have it now throws an error.

    Notice: Undefined property: sendMail in c:\phpdev5\www\alaskaerrands\index.php on line 135
    Scott Stewart
    Always happy to learn from pros.

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    $this->function(args);


  •  

    Posting Permissions

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