alkativo: Dynamic class

Hi,

how can I do something like this. With string the path is not working

$class = 'Myclassname';
$class = '\Application\Modell\'.$table;
$object = new $class($this->getAdapter());

This is working

$object = new \Application\Modell\Myclassname($this->getAdapter());

Thank you
Al

  1. Tach!

    how can I do something like this. With string the path is not working

    According to Namespaces and dynamic language features it should ...

    $class = 'Myclassname';
    $class = '\Application\Modell\'.$table;
    $object = new $class($this->getAdapter());

    ... but $class != $table. If this is not the reason please tell what "is not working" exactly means (error message).

    dedlfix.

    1. Tach!

      how can I do something like this. With string the path is not working

      According to Namespaces and dynamic language features it should ...

      $class = 'Myclassname';
      $class = '\Application\Modell\'.$table;
      $object = new $class($this->getAdapter());

      ... but $class != $table. If this is not the reason please tell what "is not working" exactly means (error message).

      dedlfix.

      Hi,

      you are right first var is $table. The $class is filled with '\Application\Modell\Myclassname' correctly.

      The error is
      Call to a member function Myclassname() on a non-object

      Because the string is not working here. I can not make the full application new now with namespaces ;(

      Thank you
      Al

      1. Hi,

        $class = '\Application\Modell\'.$table;

        I'm not familiar with OOP in PHP, but the doubled backslash looks odd to me. Is that really correct? To my knowledge, it would be masking the following single quote mark, thus produce a syntax error.
        Especially as you mention in your following post ...

        The $class is filled with '\Application\Modell\Myclassname' correctly.

        ... that the resulting string has only one backslash and the correct suffix. So I assume the double backslash is what some call a "copy & paste error". In that case, I'd ask you to be precise in posting code samples. Introducing typos in re-typing or modifying the code leads to wrong or meaningless conclusions on behalf of those trying to help.

        Bye,
         Martin

        --
        "Mutti, hier steht, das Theater sucht Statisten. Was sind Statisten?" - "Das sind Leute, die nur rumstehen und nichts zu sagen haben." - "So wie Papa?"
        Selfcode: fo:) ch:{ rl:| br:< n4:( ie:| mo:| va:) de:] zu:) fl:{ ss:) ls:µ js:(
        1. Hi,

          $class = '\Application\Modell\'.$table;

          I'm not familiar with OOP in PHP, but the doubled backslash looks odd to me. Is that really correct? To my knowledge, it would be masking the following single quote mark, thus produce a syntax error.

          No, just using ' here would cause an error, because using that .$table; would be part of the text literal as well – which n turn would thereby remain unterminated.

          \ is correct, because a pure backslash is meant here – and it has to be quoted to take away it’s special meaning of quoting the ' it would otherwise have if it was only one backslash.

          MfG ChrisB

          --
          RGB is totally confusing - I mean, at least #C0FFEE should be brown, right?
          1. Yes.

            Like i told on the sting is not the problem.

            See you AL

        2. Tach!

          $class = '\Application\Modell\'.$table;
          I'm not familiar with OOP in PHP, but the doubled backslash looks odd to me. Is that really correct?

          Yes, no problem. ' would escape the ', so \ stands for an escaped \ and then ' stands for itself. This is string behavior, has nothing to do with OOP.

          dedlfix.

          1. Hi,

            $class = '\Application\Modell\'.$table;
            I'm not familiar with OOP in PHP, but the doubled backslash looks odd to me. Is that really correct?
            Yes, no problem. ' would escape the ', so \ stands for an escaped \ and then ' stands for itself. This is string behavior, has nothing to do with OOP.

            I see. I failed to think of the backslash itself inside single quotes.
            However, then the single occurrence at \App and \Mod is wrong, strictly speaking, though it doesn't hurt in this particular situation because \A and \M don't have any special meaning.

            Bye,
             Martin

            --
            Gültig sind Frauen ab 16, wohlgeformt ab 160 Pfund.
              (Gunnar Bittersmann)
            Selfcode: fo:) ch:{ rl:| br:< n4:( ie:| mo:| va:) de:] zu:) fl:{ ss:) ls:µ js:(
            1. Tach!

              However, then the single occurrence at \App and \Mod is wrong, strictly speaking,

              Nope, as PHP defines only a meaning for both \ and ' in single quoted strings. Any other combination will be treated literally.

              dedlfix.

              1. Hi,

                However, then the single occurrence at \App and \Mod is wrong, strictly speaking,
                Nope, as PHP defines only a meaning for both \ and ' in single quoted strings.

                there it says: To specify a literal backslash, double it (\).

                Any other combination will be treated literally.

                True, I read that too. But to me, it appears like just a matter of "guaranteed fault tolerance" to accept a single backslash, even though there's a rule that says it should be doubled. It's clearly one of those wobbly rules that PHP is so rich of.

                Anyway, it means that alkativo's code is syntactically correct, I admit.
                But nevertheless I'd use double backslashes even when a single one would do. It seems cleaner to me. ;-)

                Ciao,
                 Martin

                --
                Fettflecke werden wieder wie neu, wenn man sie regelmäßig mit etwas Butter einschmiert.
                Selfcode: fo:) ch:{ rl:| br:< n4:( ie:| mo:| va:) de:] zu:) fl:{ ss:) ls:µ js:(
      2. Tach!

        The error is
        Call to a member function Myclassname() on a non-object

        Does this message really points to the line of the "new"? Could you please show a working example (means to see the error) with as less code as possible?

        dedlfix.