Welcome Guest, Not a member yet? Register   Sign In
How to call a method in the same class?
#1

[eluser]Sinclair[/eluser]
Hi,

In a controller, how to call other methods inside methods?

For example, I have the controller Newsletter, and this controller have the methods Index and Send, how can I in the method Send access variables inside the method Index?

I know this is a basic PHP OOP question, but I can't find it googgling.


Best Regards,
#2

[eluser]mikelbring[/eluser]
I am not 100% sure what your trying to accomplish, but it sounds like you need to define a variable in the class and then modify it in a function and then use it in another function for example:

Code:
class myclass{
    public $var;
    
    function myFunction(){
        $this->varr ='my variable';
    }
    
    function printVar(){
        echo $this->varr;
    }

}

I hope that is what you are looking for.

Note using "public" is php5.x, use "var" if you are on anything less.
#3

[eluser]danmontgomery[/eluser]
You can only access variables across methods if they are class members, and then they're accessed using $this
Code:
class my_class {

    public $foo = "bar";

    public function my_function() {
        echo $this->foo;
    }

}
#4

[eluser]Sinclair[/eluser]
Hi,

I have tried this way:

Code:
Newsletter::index()->data_template

It does what I want, but gives a php error:

Quote:Trying to get property of non-object

What can I do to solve this?


Best Regards,
#5

[eluser]danmontgomery[/eluser]
It doesn't work that way. Read and understand what variable scope is:

http://php.net/manual/en/language.variables.scope.php

Then do as was suggested by mikelbring, which is the correct way to access class members
#6

[eluser]Sinclair[/eluser]
Hi,

I have been reading, but I can't understant how to access the $data_template array from the index method.

Here is the code:

Code:
<?php

class Newsletter extends Controller {
      
    function Newsletter()
    {
        parent::Controller();
        
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('array');
        $this->load->helper('my_addjsfile_helper');
        $this->load->helper('my_addbodytag_helper');
        $this->load->model('Template_model');
    }
      
    function index()  
    {  
                
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_index'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Newsletter'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
                
        
        # Neste bloco defino a passagem de variáveis para a view
        $this->data_view['result2'] = null;
                
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
        $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"        
    }    
      
    
    function subscricaopasso2()  
    {  
        $this->load->library('form_validation');
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_subscricaopasso2'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Subscrição da Newsletter, Passo 2'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
              
        # Neste bloco defino a passagem de variáveis para a view
        $this->data_view['result2'] = null;
                
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
        # Neste bloco faço as validações
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view ('common/template_view', Newsletter::index()->data_template); # Aqui leio o template comum em "common/template_view.php"
        }
        else
        {
            null;
        }        
                
    }
      
  
      
}

?>


How could I access to the $data_template array of the index()?


Best Regards,
#7

[eluser]mikelbring[/eluser]
If you are trying to access it via another function do:
Code:
class Newsletter extends Controller {

public $data_array;
#8

[eluser]Sinclair[/eluser]
Hi,

I have done it, but is not working...

The data_template variable have the same name in Index() and in Subscricaopasso2(). How can I call it in the correct way?

Here is the code:

Code:
<?php

class Newsletter extends Controller {
    
    public $data_template;
      
    function Newsletter()
    {
        parent::Controller();
        
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('array');
        $this->load->helper('my_addjsfile_helper');
        $this->load->helper('my_addbodytag_helper');
        $this->load->model('Template_model');
    }
      
    function index()  
    {  
                
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_index'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Newsletter'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
                
        
        # Neste bloco defino a passagem de variáveis para a view
        #$this->data_view['result'] = $this->Tag_model->getTodasAsTags();
        #$this->data_view['result2'] = $this->Tag_model->getTodasAsTagsCloud();
        $this->data_view['result2'] = null;
                
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
        $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"        
    }    
      
    
    function subscricaopasso2()  
    {  
        $this->load->library('form_validation');
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_subscricaopasso2'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Subscrição da Newsletter, Passo 2'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
        
        
        
        # Neste bloco defino a passagem de variáveis para a view
        #$this->data_view['result'] = $this->Tag_model->getTodasAsTags();
        #$this->data_view['result2'] = $this->Tag_model->getTodasAsTagsCloud();
        $this->data_view['result2'] = null;
                
        
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
        # Neste bloco faço as validações
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view ('common/template_view', Newsletter::index()->data_template); # Aqui leio o template comum em "common/template_view.php"
            #$this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"
        }
        else
        {
            null;
        }        
                
    }
      
  
      
}

?>



Best Regards,
#9

[eluser]mikelbring[/eluser]
First of all, it does not look like you are actually using the date_template between functions, but just using it in that function it self. I might be wrong, but I am not really sure what you are trying to do.
#10

[eluser]Sinclair[/eluser]
For a better understanding, the right code should be:

Code:
<?php

class Newsletter extends Controller {
    
    public $data_template;
      
    function Newsletter()
    {
        parent::Controller();
        
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->helper('array');
        $this->load->helper('my_addjsfile_helper');
        $this->load->helper('my_addbodytag_helper');
        $this->load->model('Template_model');
    }
      
    function index()  
    {  
                
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_index'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Newsletter'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
                
        
        # Neste bloco defino a passagem de variáveis para a view
        #$this->data_view['result'] = $this->Tag_model->getTodasAsTags();
        #$this->data_view['result2'] = $this->Tag_model->getTodasAsTagsCloud();
        $this->data_view['result2'] = null;
                
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
        $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"        
    }    
      
    
    function subscricaopasso2()  
    {  
        $this->load->library('form_validation');
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'newsletter_subscricaopasso2'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Subscrição da Newsletter, Passo 2'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        
        
        
        
        # Neste bloco defino a passagem de variáveis para a view
        #$this->data_view['result'] = $this->Tag_model->getTodasAsTags();
        #$this->data_view['result2'] = $this->Tag_model->getTodasAsTagsCloud();
        $this->data_view['result2'] = null;
                
        
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        
        # Neste bloco faço as validações
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view ('common/template_view', Newsletter::index()->data_template); # Aqui leio o template comum em "common/template_view.php"
        }
        else
        {
            $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"
        }        
                
    }
      
  
      
}

?>

So I should read the $data_template of the index() or subscricaopasso2() :
Code:
# Neste bloco faço as validações
        $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view ('common/template_view', Newsletter::index()->data_template); # Aqui leio o template comum em "common/template_view.php"
        }
        else
        {
            $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"
        }

How should to this?

Best Regards,




Theme © iAndrew 2016 - Forum software by © MyBB