K
K
kirillnikola2014-10-01 17:52:02
PHP
kirillnikola, 2014-10-01 17:52:02

Why are parameters not being passed to the php method?

Good evening, explain to a newbie, there are two files and the situation is as follows:

<?php
class Myclass 
{
    function mymethod($par1,$par2) 
    {
    include 'add/'.$par1;       
    }
}
 
class MyClass2
{   
 
    function action_index() {
        $this->var1 = new Myclass();
        $par1 = 'ex3.php';
        $par2 = 123;
        $this->var1->mymethod($par1,$par2);
 
    }
}
$child = new MyClass2();
$child->action_index();
 
    
?>

and the second ex3.php file
<?php
    echo $par2;
?>

the method in the first class takes two values ​​$par1,$par2, in the second class I declare properties of the same name and and put them in the first class method, but if I change the names of the properties ($par1,$par2) and put them in the method then it will not work even though the values ​​remain the same, explain why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Entelis, 2014-10-01
@DmitriyEntelis

I did not understand the question. Your code works, outputs 123.
If done

<?php
class Myclass 
{
    function mymethod($par3,$par4) 
    {
    include 'add/'.$par3;       
    }
}
 
class MyClass2
{   
 
    function action_index() {
        $this->var1 = new Myclass();
        $par1 = 'ex3.php';
        $par2 = 123;
        $this->var1->mymethod($par1,$par2);
 
    }
}
$child = new MyClass2();
$child->action_index();

and your ex3.php
<?php
    echo $par4;
?>

Again, everything works as it should.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question