priority
Standard
status
Closed
resolution
Fixed

qcodo
0.4.11 (development)
category
QForm / QControls (PHP)
php
Any

Votes
There are no votes for this issue.

reported: tuesday, january 12, 2010, 8:07 pm pst  |  by: Fernando Lordán  |  messages: 9 messages  |  last: about 25 days ago


#1  |  Fernando Lordán (Barcelona, CAT, Spain) Spain
Tuesday, January 12, 2010, 8:07 PM PST

Around lines 160 and 260 of QFormBase.class.php, in the Run() method, the current working form is “globalized”. I really see no need for this, as there can only be one QForm per page and the $_FORM global is used when needed.

But the first strange thing is that it's innecessarily “globalized” twice. Twice when the form is loaded and twice when it's created. I guess it's a double typo.

QFormBase.class.php

<?php
[...]

if (
$objClass) {
    global $
$strFormId;
    $
$strFormId $objClass;

    
$objClass->strCallType $_POST['Qform__FormCallType'];
    
$objClass->intFormStatus QFormBase::FormStatusUnrendered;

    if (
$objClass->strCallType == QCallType::Ajax)
        
QApplication::$RequestMode QRequestMode::Ajax;

    
// Globalize and Set Variable
    
global $$strFormId;
    $
$strFormId $objClass;

[...]

    global $
$strFormId;
    $
$strFormId $objClass;

    
// By default, this form is being created NOT via a PostBack
    // So there is no CallType
    
$objClass->strCallType QCallType::None;

    
$objClass->strFormId $strFormId;
    
$objClass->intFormStatus QFormBase::FormStatusUnrendered;
    
$objClass->objControlArray = array();
    
$objClass->objGroupingArray = array();

    
// Globalize and Set Variable
    
global $$strFormId;
    $
$strFormId $objClass;

[...]

?>

The second thing is stranger. It seems to me that in the QFormBase::Run() static method all the QForm instance properties are accessed using its “internal” name. In fact it works, but... how are protected instance properties accessed from a static method?

#2  |  ‹‹ Qcodo System Message ››
Tuesday, January 19, 2010, 3:51 PM PST

Fernando Lord made content edits to the issue

#3  |  Mike Ho (Sunnyvale, CA) United States of America Qcodo Administrator
Thursday, April 22, 2010, 10:48 AM PDT

Thanks for the post.  This should now be fixed and has been committed to http://github.com/qcodo/qcodo/commit/45c7715583ff145aaf123b8411c4eb9d55c7361f

This should be in the 0.4.15 release.

#4  |  ‹‹ Qcodo System Message ››
Thursday, April 22, 2010, 10:48 AM PDT

Mike Ho made edits to the issue, including:

  • Status changed from New Issue to Fixed
#5  |  Fernando Lordán (Barcelona, CAT, Spain) Spain
Thursday, May 6, 2010, 1:15 PM PDT

Maybe the last part of the issue report was a little hidden after the code:

As seen in the code snippet, it seems that in the QFormBase::Run() static method all the QForm instance properties are accessed using its “internal” protected name. In fact it works, but... how are protected instance properties accessed from a static method?

Magic? PHP bug? Something escapes to my knowledge here.

#6  |  Mike Ho (Sunnyvale, CA) United States of America Qcodo Administrator
Tuesday, May 18, 2010, 6:45 AM PDT

Not sure if you are referring to variable access level protection or if you are referring to accessing instance variables within a static method context, but I'll address both.

Since the static method is a member of QFormBase, any private or protected variable access for QFormBase variables within that method is allowed.  This is similar to having a object method access protected or private variables within another object of the same class (which is also allowed):

<?php
    
class SomeClass {

        
/**
         * @var string $strPrivate
         */
        
private $strPrivate;

        public function 
Compare(SomeClass $objToCompareAgainst) {
            return (
$this->strPrivate == $objToCompareAgainst->strPrivate);
        }
    }
?>

In terms of accessing instance variables, you are right in that you cannot access instance variables belonging to “$this” within the static method context -- but note that Run() accesses the instance variables of $objClass and NOT of “$this”.  $this is never used.

#7  |  ‹‹ Qcodo System Message ››
Tuesday, May 18, 2010, 2:21 PM PDT

Mike Ho made edits to the issue, including:

  • Status changed from Fixed to Closed
  • Resolution changed from none to Fixed
#8  |  Fernando Lordán (Barcelona, CAT, Spain) Spain
Monday, January 9, 2012, 8:39 PM PST

Sorry, it's been a very long time since I wrote this question and I still hadn't read the answer.

I was referring to access level protection “violation”, like this:

$objClass->strFormId

It seems strange to me to see code accessing the internal PROTECTED properties of an ordinary instance in a context unrelated to that specific instance (not “$this”, not “self”, not “static” or “parent”...). It's completely normal when accessing properties related to the current context (using shortcuts like “$this”, “self”...), but not in an instance not related to the current context.

For instance:

<?php
class MyClass {

    protected 
$strProperty;

    public function 
MyMethod($mixValue) {

        
// This is totally normal.
        
$this->strProperty $mixValue;

        
// But this is weird, as we're accessing a protected property of OTHER instance.
        
$objInstance = new MyClass();
        
$objInstance->strProperty $mixValue;

    }
}
?>

I couldn't imagine that was allowed. From my point of view, using protected properties of an ordinary object instance from a static method (or from any other context external to the instance being accessed) is a context abuse and should not be permitted by PHP, as it violates the “black box” nature of objects in OOP.

#9  |  Mike Ho (Sunnyvale, CA) United States of America Qcodo Administrator
Monday, January 9, 2012, 11:02 PM PST

It's a good point, but actually, it's not a violation by most folks' definition of the Protected access restriction.

Obviously Wikipedia is not necessarily the end-all, be-all “source” of information =), but at least it's a good start:

protected (or class-protected) allows the class itself and all its subclasses to access the member.

Note that the definition specifies that the class, not just instances of the class, is permitted to access protected member variables, methods, etc.

And actually, it's interesting to note that most OO-based languages (including C++, C#, etc.) do allow this level of access.  Not sure about Java, but I would suspect Java follows the same rule here, as well.



Copyright © 2005 - 2012, Quasidea Development, LLC
This open-source framework for PHP is released under the terms of The MIT License.