0

I have a class

Class MyClass{

    private $prop1;
    private $prop2; // this contains an array object

    public function __construct(){ }

    public function set_prop2($prop2)  
    {  
        $this->prop2= $prop2;  // value coming from other class that returns array object
    }

    public function func1()
    {
            global $prop1; 
            global $prop2;

        $var1 = // some value;
        $var2 = $prop1;
        $var3 = // some value;
        $obj = new SomeClass($prop2);
        $json = $obj->setVar2($var2)
                         ->someMethod($var1, $otherMethod)
                         ->method();


    } 

}

using global properties inside method, the class above works for me by calling

obj1 = new MyClass();
obj1->func1(); // I want to call it like this (without parameters)

I know declaring global inside the method is not a good thing but its the only way I can make the method works!

any ideas how I can better the syntax so I can call the method without parameters like obj1->func1(); is greatly appreciated.

NOTE: tried to declare as

$this->prop1;
$this->prop2;

but it gives me a Catchable fatal error: Argument 1 passed to... because $prop2 was not passed properly if using $this->prop2;

LohanHill1
  • 11
  • 4
  • try $this->prop1; $this->prop2; – Abhik Chakraborty Jan 27 '14 at 19:35
  • @AbhikChakraborty read my note above. tried that already and no luck. – LohanHill1 Jan 27 '14 at 19:36
  • You had a $ something as $this->$prop1 and check what I wrote :) – Abhik Chakraborty Jan 27 '14 at 19:38
  • it was a typo. I did use `$this->prop1;` but still no luck. – LohanHill1 Jan 27 '14 at 19:40
  • possible duplicate of [Use global variables in a class](http://stackoverflow.com/questions/11923272/use-global-variables-in-a-class) – PeeHaa Jan 27 '14 at 19:46
  • can you please show us the whole error message? Where is func1() called? You try to access a private method. Check your scope. Set default values to your property if they doesn't need to be initialized first. Offtopic: you have a lot of bad practice and coding style in your class. – SenseException Jan 27 '14 at 19:55
  • $this->set_prop2(something); new SomeClass($this->prop2) – Royal Bg Jan 27 '14 at 19:56
  • @SenseException it was a typo. the method was public when i test it. – LohanHill1 Jan 27 '14 at 19:58
  • Since you didn't show us the whole error message I'll take a guess: the class SomeClass expects a specific value (an array?) but gets null. What happens when you use $this->prop2 again but in the head of your class "private $prop2 = array()"? Please reconstruct your error for us: http://3v4l.org/abhZY – SenseException Jan 27 '14 at 20:29
  • You have reposted your question (as well as editing [the original](http://stackoverflow.com/questions/21389278/php-how-to-access-private-class-property-inside-a-method-without-specifying-par)), but you have still not really explained the problem: 1) What is the actual error message you are seeing? 2) **Why** do you want to call the method with no parameters? (And where do you expect the values to come from if you do?) – IMSoP Jan 28 '14 at 18:51
  • Also, just to check, by "array object", do you actually mean an array, or some object that has array-like behaviour (`ArrayAccess`/`Iterator`/etc)? The full error message might give us a clue here. – IMSoP Jan 28 '14 at 19:02

3 Answers3

0
Class MyClass{

    public $prop1;
    public $prop2; // this contains an array object

    public function __construct(){ }

    public function set_prop2($prop2)  
    {  
        $this->prop2= $prop2;  // value coming from other class that returns array object
    }

    public function func1()
    {

        $var1 = // some value;
        $var2 = $this->prop1;
        $var3 = // some value;
        $obj = new SomeClass($this->prop2);
        $json = $obj->setVar2($var2)
                         ->someMethod($var1, $otherMethod)
                         ->method();


    } 

}
Jorge Barata
  • 2,227
  • 1
  • 20
  • 26
0

You are confusing 3 different things here:

  • Global variables, which exist outside any function, class, or object, have to be "imported" into the current scope with the global keyword. They are generally considered bad practice as they lead to code which "magically" transports data from one place to another, making reuse, testing, and debugging more difficult.
  • Object properties are declared in a class definition, and belong to an instance of that class (or to the class itself, in the case of static variables). They are accessed with the syntax $object->property. The private keyword means the variable can only be seen inside the class, by using the special variable $this, meaning "this object I am inside", e.g. echo $this->prop1;
  • Function arguments are passed in when you call a function. Inside the function, they are local variables with the names given in the function definition, so do not need to be imported or prefixed in any way. Object methods are basically just functions "attached to" a particular object, and work in the same way.

In your example, you have a comment against an object property here...

 private $prop2; // this contains an array object

...but you then say that using that variable (with $this->prop2) gave an error (which you haven't actually shown us properly yet) suggesting that it was not an array. (As mentioned in the comments, "array object" is an odd choice of words. I'm going to assume you just meant "array" for now.)

You then "solve" this by instead using a global variable...

global $prop2;
$obj = new SomeClass($prop2);

...which is a completely different variable.

You also show a definition of a method for setting your private property...

public function set_prop2($prop2)

...but you never show us where you're calling it.

You've annotated it with a comment suggesting where you think it's being called...

// value coming from other class that returns array object

...but the entire question hinges on where that function argument comes from.


One of the things that makes your code hard to read is that the object property, the global variable, and the function argument all have the same name. This doesn't make them the same variable.

What you could try is separating out the different variables, and looking at each in turn:

class MyClass {
    private $private_property;

    public function set_prop2($new_value_for_prop2) {  
        var_dump($new_value_for_prop2); // what's the value passed in?

        $this->private_property = $new_value_for_prop2;
    }

    public function func1() {
        var_dump($this->private_property); // what value is it now?
    } 
}

$function_return = some_function();
var_dump($function_return); // is it an array?

$obj = new MyClass;
$obj->set_prop2($function_return); // should show you the value being set

$obj->func1(); // should show the same value again
IMSoP
  • 89,526
  • 13
  • 117
  • 169
-1

You can use session functions but I'm not sure that is a good idea for the security... Depend the critical of the parametres...

<?php
Class MyClass{

    private $prop1;
    private $prop2; // this contains an array object

    public function __construct(){ }

    public function set_prop2($prop2)  
    {  
        $this->prop2= $prop2;  // value coming from other class that returns array object
    }

    private function func1()
    {
        $prop1 = $_SESSION['prop1']; 
        $prop2 = $_SESSION['prop2'];

        $var1 = // some value;
        $var2 = $prop1;
        $var3 = // some value;
        $obj = new SomeClass($prop2);
        $json = $obj->setVar2($var2)
                     ->someMethod($var1, $otherMethod)
                     ->method();
    }

}
?>

And for calling :

<?php
session_start();
$_SESSION['prop1'] = $prop1;
$_SESSION['prop2'] = $prop2;
obj1 = new MyClass();
obj1->func1();
?>
Georgio
  • 149
  • 3
  • This doesn't really make any sense in the context of the question - you seem to be using `$_SESSION` as just another way of transporting the global variables. In many ways, you're making them "more" global, in that they'll persist between requests. – IMSoP Jan 28 '14 at 18:53