Jump to content

[SOLVED] Fatal error: Call to member function on...non-object...


Recommended Posts

I'm back again...making slow progress on my class building experience, but everything seems very painful in PHP...

 

Keep getting the fatal error when I try to call a function from a class to another class. The identical syntax is working on a previous line in the class. There's a lot of code in this process; I'll try  to post just enough...

 

main class


<?php
class declaration {
......
include_once 'wp_form.cls.php';
// triple+ checked class spelling and path etc
........  
private $wpform; 
private $method;
private $action; 
.......  
  function set_method($m){
    if (isset($m)){
      $this->method = $m;
    }
    else{
      debug_var('method','->no value for $method in wpc set_method<-', $m); // debug_var is a function I wrote for debugging - not the issue     } 
  }
  function set_action($a){
    if (isset($a)){
      $this->action = $a;
    }
    else{
      debug_var('action','->no value for $action in wpc set_action<-', $a);
    } 
  }
  function get_form(){
    $wpform = new wp_form;
//problem is here vvvvvvvv
    $this->wpform = $this->wpform->get_wp_form($this->method, $this->action, $this->wptable);
} ?>

 

wp_form class (entire class)

 

<?php class wp_form{

  function get_wp_form($m, $a, $t){ // method, action, table      $wpform = '<form method ="' . $m . '" action="'. $a . '">';
    $wpform.= $t;
    $wpform.= '</form>';

    return $wpform; 
  }
} ?> 

 

section of php script that sets the object and calls the function

 
<? php
...........
  //method   $m = 'post'; //action   $a = 'addemail.php';
  $new_wp->set_method($m);
  $new_wp->set_action($a); 
  $new_wp->get_form();
........ ?>

 

$method and $action are setting correctly and $this->wptable is getting populated in an earlier step using similar methodology. I've verified that the object wpform is getting created as well. [/]

The syntax I'm using to populate wpform seems like it's more than I need, but it's the only syntax taht has worked in previous similar sections of the same class. I'm sure this is something stupid, but I can't find any syntax issues.[/]

 

PHP 5.3.0 if it matters...

 

(the overall project is to build a dynamic webpage. I know there's probably a thousand scripts out there that already do this, but I'm just busting my own chops here for practice)

Ahh okay I see the problem now, it's with scope.

 

function get_form(){
    $wpform = new wp_form;
    //problem is here vvvvvvvv
    $this->wpform = $this->wpform->get_wp_form($this->method, $this->action, $this->wptable);
}

$wpform's scope is local to that function, where as $this->wpform's scope is the class

 

It should read:

function get_form(){
    $this->wpform = new wp_form;
    //problem is here vvvvvvvv
    $this->wpform = $this->wpform->get_wp_form($this->method, $this->action, $this->wptable);
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.