[campsite-dev] design pattern to safely detect static method call
  • Hi,

    does somebody know if there is an way to test within an method if call was
    statically or not? I googled like for "design pattern php static method
    call", but did not found what I searched for.


    During debugging I have seen that $this is even set on static method call,
    if the called came from "within" another object.

    Like those lines will output "Foo":

    class Foo {
    function method()
    {
    Bar::method();
    }
    }

    class Bar
    {
    function method()
    {
    echo (get_class($this));
    }
    }

    $x = new Foo();
    $x->method();
    ?>



    Thats why contruct like following could be problematic. Imagine: call was
    statically from another object having also property m_data['Number'], and by
    some situation parameter $p_articleNumber was null.

    function getTranslations($p_articleNumber = null)
    {
    if (!is_null($p_articleNumber)) {
    $articleNumber = $p_articleNumber;
    } elseif (isset($this)) {
    $articleNumber = $this->m_data['Number'];
    } else {
    return array();
    }
    $queryStr = 'SELECT * FROM Articles '
    ." WHERE Number=$articleNumber";
    $articles = DbObjectArray::Create('Article', $queryStr);
    return $articles;
    } // fn getTranslations


    Best,
    Sebastian
  • 8 Comments sorted by
  • hi Seb !

    i dont know such a thing, anyway if you are using PHP5 you can
    specifically declare methods as static and avoid that kind of
    problems. try this:

    class Foo
    {
    public function method()
    {
    Bar::method();
    }
    }

    class Bar
    {
    public static function method()
    {
    echo (get_class($this));
    }
    }

    $x = new Foo();
    $x->method();


    greetz!


    On 8/24/07, Sebastian Goebel wrote:
    > Hi,
    >
    > does somebody know if there is an way to test within an method if call was
    > statically or not? I googled like for "design pattern php static method
    > call", but did not found what I searched for.
    >
    >
    > During debugging I have seen that $this is even set on static method call,
    > if the called came from "within" another object.
    >
    > Like those lines will output "Foo":
    >
    > > class Foo {
    > function method()
    > {
    > Bar::method();
    > }
    > }
    >
    > class Bar
    > {
    > function method()
    > {
    > echo (get_class($this));
    > }
    > }
    >
    > $x = new Foo();
    > $x->method();
    > ?>
    >
    >
    >
    > Thats why contruct like following could be problematic. Imagine: call was
    > statically from another object having also property m_data['Number'], and by
    > some situation parameter $p_articleNumber was null.
    >
    > function getTranslations($p_articleNumber = null)
    > {
    > if (!is_null($p_articleNumber)) {
    > $articleNumber = $p_articleNumber;
    > } elseif (isset($this)) {
    > $articleNumber = $this->m_data['Number'];
    > } else {
    > return array();
    > }
    > $queryStr = 'SELECT * FROM Articles '
    > ." WHERE Number=$articleNumber";
    > $articles = DbObjectArray::Create('Article', $queryStr);
    > return $articles;
    > } // fn getTranslations
    >
    >
    > Best,
    > Sebastian
    >
    >
    >
    >
    >
    >


    --
    /holman
  • You are right Holman, but I would like to be to be able that function can be
    called statically AND as object method.

    Currently I use the other way around to detect which way the function was
    called, which allows to call the method statically even from within object
    of own class:

    example:

    public function triggerStatistics($p_poll_nr = null)
    {
    if (!is_null($p_poll_nr)) {
    $poll = new Poll(null, $p_poll_nr);;
    } elseif (isset($this)) {
    $poll = $this;
    }

    (...)

    }
  • Hi Sebastian,

    Please create 2 separate methods: a static one and an object method. It's
    not correct to implement a single method that may behave differently
    depending on how it was called. There is no reason to implement a single
    method for both purposes. In PHP 5 the reserved word 'static' is exactly for
    this purpose, please do not circumvent the OO programming concepts, this
    will make our work harder in the future.

    Mugur

    On 8/24/07, Sebastian Goebel wrote:
    >
    > You are right Holman, but I would like to be to be able that function can
    > be
    > called statically AND as object method.
    >
    > Currently I use the other way around to detect which way the function was
    > called, which allows to call the method statically even from within object
    > of own class:
    >
    > example:
    >
    > public function triggerStatistics($p_poll_nr = null)
    > {
    > if (!is_null($p_poll_nr)) {
    > $poll = new Poll(null, $p_poll_nr);;
    > } elseif (isset($this)) {
    > $poll = $this;
    > }
    >
    > (...)
    >
    > }
    >
    >
  • Hi Mugur,

    method overloading (or however this is called) is not possible in PHP5 Sad There is something like this, but it's something different.

    I could use 2 method names to solve that, and use the magic __call() method to decide between boot, but at the end this is same as I do it within the original method.

    Best,
    Sebastian



    -----Original Message-----
    From: Mugur Rus [mailto:mugur.rus@gmail.com]
    Sent: Monday, August 27, 2007 3:53 PM
    To: campsite-dev@campware.org
    Subject: Re: [campsite-dev] design pattern to safely detect static method call


    Hi Sebastian,

    Please create 2 separate methods: a static one and an object method. It's not correct to implement a single method that may behave differently depending on how it was called. There is no reason to implement a single method for both purposes. In PHP 5 the reserved word 'static' is exactly for this purpose, please do not circumvent the OO programming concepts, this will make our work harder in the future.

    Mugur


    On 8/24/07, Sebastian Goebel wrote:
    You are right Holman, but I would like to be to be able that function can be
    called statically AND as object method.

    Currently I use the other way around to detect which way the function was
    called, which allows to call the method statically even from within object
    of own class:

    example:

    public function triggerStatistics($p_poll_nr = null)
    {
    if (!is_null($p_poll_nr)) {
    $poll = new Poll(null, $p_poll_nr);;
    } elseif (isset($this)) {
    $poll = $this;
    }

    (...)

    }
  • Hi Sebastian,

    I'm not talking about method overloading, give them different names. Or,
    there is another possibility: create a single static method with a parameter
    $object. If the parameter was null then proceed as a static method. If the
    parameter was an object then proceed as a regular method.

    Mugur

    On 8/28/07, Sebastian Goebel wrote:
    >
    > Hi Mugur,
    >
    > method overloading (or however this is called) is not possible in PHP5 Sad
    > There is something like this, but it's something different.
    >
    > I could use 2 method names to solve that, and use the magic __call()
    > method to decide between boot, but at the end this is same as I do it within
    > the original method.
    >
    > Best,
    > Sebastian
    >
    >
    >
    > -----Original Message-----
    > *From:* Mugur Rus [mailto:mugur.rus@gmail.com]
    > *Sent:* Monday, August 27, 2007 3:53 PM
    > *To:* campsite-dev@campware.org
    > *Subject:* Re: [campsite-dev] design pattern to safely detect static
    > method call
    >
    > Hi Sebastian,
    >
    > Please create 2 separate methods: a static one and an object method. It's
    > not correct to implement a single method that may behave differently
    > depending on how it was called. There is no reason to implement a single
    > method for both purposes. In PHP 5 the reserved word 'static' is exactly for
    > this purpose, please do not circumvent the OO programming concepts, this
    > will make our work harder in the future.
    >
    > Mugur
    >
    > On 8/24/07, Sebastian Goebel wrote:
    > >
    > > You are right Holman, but I would like to be to be able that function
    > > can be
    > > called statically AND as object method.
    > >
    > > Currently I use the other way around to detect which way the function
    > > was
    > > called, which allows to call the method statically even from within
    > > object
    > > of own class:
    > >
    > > example:
    > >
    > > public function triggerStatistics($p_poll_nr = null)
    > > {
    > > if (!is_null($p_poll_nr)) {
    > > $poll = new Poll(null, $p_poll_nr);;
    > > } elseif (isset($this)) {
    > > $poll = $this;
    > > }
    > >
    > > (...)
    > >
    > > }
    > >
    > >
    >
  • Hi,

    can you (mainly Holman and Mugur) tell me if there is one of the list_ statements / ListXxx.php class is already implemented, so I could use it as example for poll? To keep it similar, I would need to see how you do it.

    Thanks,
    Sebastian
  • Hi Sebastian,

    Please check the class IssuesList in site/template_engine/classes. This
    class inherits ListObject. Also, read the function
    smarty_block_list_issues() in
    site/include/smarty/camp_plugins/block.list_issues.php

    Mugur

    On 9/7/07, Sebastian Goebel wrote:
    >
    > Hi,
    >
    > can you (mainly Holman and Mugur) tell me if there is one of the list_
    > statements / ListXxx.php class is already implemented, so I could use it
    > as example for poll? To keep it similar, I would need to see how you do it.
    >
    > Thanks,
    > Sebastian
    >
    >
  • Also, you have an example of the template language in the file
    site/template_engine/camp_index.tpl

    Mugur

    On 9/7/07, Mugur Rus wrote:
    >
    > Hi Sebastian,
    >
    > Please check the class IssuesList in site/template_engine/classes. This
    > class inherits ListObject. Also, read the function
    > smarty_block_list_issues() in
    > site/include/smarty/camp_plugins/block.list_issues.php
    >
    > Mugur
    >
    > On 9/7/07, Sebastian Goebel wrote:
    > >
    > > Hi,
    > >
    > > can you (mainly Holman and Mugur) tell me if there is one of the list_
    > > statements / ListXxx.php class is already implemented, so I could use it
    > > as example for poll? To keep it similar, I would need to see how you do it.
    > >
    > > Thanks,
    > > Sebastian
    > >
    > >
    >