-1

I found out that this work:

$var = 'value';

$obj = new myClass();

class myClass{
  function __construct(){
    $this->myFunc($var);
  }
  public function myFunc($var){
    echo $var;
  }
}

But is it a good pratice ? Or i should just do it like this?

$var = 'value';

$obj = new myClass($var);

class myClass{

  public $var;

  function __construct($var){
    $this->var = $var;
    $this->myFunc($this->var);
  }

  public function myFunc($var){
    echo $var;
  }
}

Or,if both can be used , when it's recommended to use one and when, the other? Also , is there a good website,blog,topic,tutorial about best practices for php including oop ?

EDIT: Seems like using variables does not output the value,but does not give an error neither.My initial code use constants and i thought it would also work with variables:

define('blabla','value');

$obj = new myClass();

    class myClass{
      function __construct(){
        $this->myFunc(blabla);
      }
      public function myFunc($var){
        echo $var;
      }
    }
Petru Lebada
  • 2,167
  • 8
  • 38
  • 59
  • http://www.phptherightway.com/ for the resources about PHP and OOP. The 2nd way of is far better than the first one, because one can read it without thinking. The 1st way, might be faster to execute, so it depends on what you want. And I'm sure you could find a 3rd way, even faster. – Unex Aug 18 '16 at 09:46
  • @Unex , i've read this website , there i found the 2nd way but it does not tell why i shouldn't use some other ways for doing this.I also think the first solution works faster , that's why i wanted to hear some pros and cons – Petru Lebada Aug 18 '16 at 09:54
  • 2
    Your first example does not 'works'. – Progrock Aug 18 '16 at 09:58
  • Well, readability is an important part of programming. In the 2nd way you know directly at a glance what is happening, and not in the 1st way. Your example is simple, but on a complex class, this would be totally unreadable. Generally, before making code that is fast, we make code that is readable to help maintenance and reduce debt over time. But, if performance starts to be needed, then the code is often refactored into a less readable, but more fast one. Don't overthink your code to be fast, overthink it to be clear, extensive and easy to test. – Unex Aug 18 '16 at 09:59
  • @Unex, uhmm ... good point – Petru Lebada Aug 18 '16 at 10:04
  • @PetruLebada have you got some magic implementation of Php that forgos the correct declaration of functions/methods inside classes? TEST YOUR CODE! – Progrock Aug 18 '16 at 10:13
  • You should replace `public myFunc($var){` with `public function myFunc($var){` – Unex Aug 18 '16 at 10:49
  • @Progrock , my bad, i was in a hurry and forgot to edit the question after i've test it. – Petru Lebada Aug 18 '16 at 11:02
  • go look up encapsulation. that should give you most of what you need in order to decide how to handle variables. – DevDonkey Aug 18 '16 at 11:04
  • @DevDonkey , encapsulation isn't just for properties/methods ? And i'm not sure of what use can be here... – Petru Lebada Aug 18 '16 at 11:08
  • `function __construct() { $this->myFunc($var); }` -- who is `$var`? There is no such variable defined in the local context of `__construct()`. – axiac Aug 18 '16 at 11:08
  • What you are describing is called an Upvalue, and it is a great feature in many languages, but PHP is NOT one of them. Don't do this in PHP. – Erik Aug 18 '16 at 11:18
  • Closed this as duplicate because the basic premise of the question is wrong. See http://stackoverflow.com/a/12446305/476 and many similar topics for why *not* to use implicit variables of any kind. – deceze Aug 18 '16 at 11:24
  • BTW, this *would* give you an error (well, a *notice*), if you actually enabled error reporting. – deceze Aug 18 '16 at 11:26

1 Answers1

0

You should try something like this .

$myVar = 'myvalue';

class myClass {
  private $class_var;

  public function __construct($myVar) {
    $this->class_var=$myVar;
  }

  //REST OF CLASS BELOW
}

So when declaring the object, you will need to pass $myVar

$myClass = new myClass($myVar);
  • Alright,but why shouldn't i use the first solution,if it works ? Also less to write ... – Petru Lebada Aug 18 '16 at 09:55
  • 1
    Because php is changing a lot and in upcoming versions this might be deprecated. – Vikrant Vir Bhalla Aug 18 '16 at 09:56
  • Vik, yes but that is also true for your solution ... that's not up to us what will be deprecated in the upcoming versions. – Petru Lebada Aug 18 '16 at 10:07
  • Php was a very forgiving language before Version 7 . But now in Php 7 the deprecate methods dont work at all. :) – Vikrant Vir Bhalla Aug 18 '16 at 10:12
  • @VikrantVirBhalla what is the deprecation you are referring to? – Progrock Aug 18 '16 at 10:35
  • The reason you should not use example 1 (assuming it even worked) is that if anyone else instantiates your class, it will fail because they might not have a $var in the outer scope and they will be very confused why you are referencing it, when classes are designed so that you don't have to do such things. – Erik Aug 18 '16 at 11:19