Jump to content

Inheritence Problem.. (i think?)


Recommended Posts

Hello,

 

I am new to PHP and was writing these classes to convert HTML elements into objects. Everything seems to have gone good until attempting to send the attributes to a variable parameter function, which after testing all values were passed correctly and even are valid when testing the attribute name given with the corresponding property name. These are passed as: $object->attributes("attribute1=value1","attribute2=value2"). Once the arguments have reached the parent class, they are then checked to see if the attribute value given matches any of the objects property names. If so, the value is set for the property.

 

Basically, only the type="checkbox" is being set. I set some echo's to display what my problem is :P

 

 

Thanks for your time :)

 

- Trevor Kavanaugh

 

 

<?php
class Control
{
    #attributes
    protected $class;
    protected $id;
    protected $style;
    protected $name;
    
    #methods
    public function insert()                                    // SPILLS HTML TO BROWSER
    {
        foreach($this as $attribute => $value)
        {
            if($value !== NULL)
                echo " $attribute=\"$value\"";
            
        }
    }
    public function attributes()                                           // SETS PROPERTY VALUES
    {
        $args = func_get_arg(0);                                        // A single array was passed containing all arguments
                                                                        // only FINAL children::attributes() will be passed
                                                                        // a variable number of arguments.
                                                                        
        
        foreach($this as $property => $value)
        {
            echo "<br /><b>$property</b>";                         // Outputs real property name
            foreach($args as $index => $setval)
            {
                list($att, $val) = split("=", $setval);
                if($property == $att)
                {
                    echo "<br />$att = $val";                             // Outputs values passed as arguments (property = value)
                    $$property = $val;
                }
            }
        }
        
        ############# OUTPUT TESTS #############################################
        echo "<br><br><b>OUTPUT TESTING:</b>";
        echo "<br />My Type Is: $this->type!!";                    # WORKS
        echo "<br />My Name Is: $this->name!!";                    # DOESNT WORK
        echo "<br />My Class Is: $this->class!!";                # DOESNT WORK
        ########################################################################
    }
}
class Input extends Control
{
    #attributes
    protected $disabled;
    protected $value;
    protected $tabindex;
    protected $accesskey;
    
    #methods
    public function insert()
    {
        echo '<input';
        parent::insert();
        echo '>';            // INSERT CLOSING BRACKET HERE
    }
    public function attributes()
    {
        parent::attributes(func_get_arg(0));
    }
}
FINAL class Checkbox extends Input
{
    #attributes
    protected $checked;
    protected $type = "checkbox";
    
    #methods
    public function insert()
    {
        parent::insert();
    }
    public function attributes()
    {
        parent::attributes(func_get_args());
    }
}


$form = new Checkbox();
$form->attributes("type=checkbox", "name=myname", "class=myclass");
?>

Link to comment
https://forums.phpfreaks.com/topic/241341-inheritence-problem-i-think/
Share on other sites

Hehe, silly me just took a shower and realized I had type set to "checkbox" in the child element. Took that off and it appears none of the real properties are being set. I think its on line 93: $$property = $val;

 

All help still greatly appreciated :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.