hi all,
i have a problem with function_exists
i call it in a object function, but it doesnt work the way i thought

the functions are both defined, but i get always false as result...

class myObject
{
    function insert()
    {
        $sql = '';
        $valueSql = '';
        //...

    // add creation date if needed
    if (function_exists("getCreated"))
    {
        $sql .= ", created";
        $valueSql .= ", '" . date("Y-m-d H:i:s") . "'";
    }
    echo function_exists("getCreated") . " + " . function_exists("insert") . "***";
}
}

thx for help,

steffel

    Simplify your example and give it in full.
    Obviously in your current code snippet both will return false since getCreated() doesnt exist.

      i call the function insert from an object that inherits this function from myObject.
      the called object GOT a function called getCreated
      also myObject got the function insert, so both method calls of function_exists should return true, or?

        require_once($GLOBALS['SYSTEMBEANPATH'] . "/class.base_bean.php");
        
        /**
         * Allows function setting and getting of all given database fields of this db-table.
         */
        class Person extends BaseBean
        {
            var $created;
        
           /**
            * @return string
            */
            function getCreated()
            {
                return $this->getData("created");
            }
           /**
            * @param string
            */
            function setCreated($my_created)
            {
                $this->setData("created", $my_created);
            }
        
           /**
            * writes entry to database
            */
            function save()
            {
                if ($this->isChanged())
                {
                    if ($this->getId() != -1)
                        $this->update();
                    else {
                        $this->setStatus(1);
        
                    // call of the insert function of BaseBean
                    $this->insert();
                }
            }
        }
        }
        
        
        
        /**
         * Base methods and variables for all beans
         */
        class BaseBean extends DBMapping
        {
           /**
            * insert new entry into table,
            * reset changings
            * and write entry to log file
            */
            function insert()
            {
                $values = $this->getChanged();
                $fieldnames = array_keys($values);
        
            $sql = "INSERT INTO " . $this->getTable($this->myTable) . " (";
            $valueSql = "";
            $i = 0;
        
            foreach($fieldnames as $currentName)
            {
                if ($i > 0) {
                    $sql .= ", ";
                    $valueSql .= ", ";
                }
        
                $sql .= $currentName;
                if ($currentName == "password")
                    $valueSql .= "PASSWORD('" . $values[$currentName] . "')";
                else if (is_array($values[$currentName]))
                    $valueSql .= "'" . implode(",",$values[$currentName]) . "'";
                else
                    $valueSql .= "'" . $values[$currentName] . "'";
                $i++;
            }
        
            // add creation date if needed
            if (function_exists("getCreated"))
            {
                $sql .= ", created";
                $valueSql .= ", '" . date("Y-m-d H:i:s") . "'";
            }
        echo function_exists("getCreated") . " + " . function_exists("insert") . "***";
                $sql .= ") VALUES (" . $valueSql . ")";
                dbi_query($sql, get_class($this));
        
            // if id exists get newest inserted id from database
            if ($this->id) {
                $myId = dbi_getInsertedId();
                $this->setId($myId);
            }
            if ($_SESSION["ses_person"])
                $myPersonId = $_SESSION["ses_person"]->getId();
        
            $this->resetChanged();
            $this->logdata = $GLOBALS["myLogHandler"]->formatTime(time()) .  $GLOBALS["myLogHandler"]->getSeparator() . get_class($this) . $GLOBALS["myLogHandler"]->getSeparator() . $myPersonId . $GLOBALS["myLogHandler"]->getSeparator() . $_SERVER["REMOTE_ADDR"] . $GLOBALS["myLogHandler"]->getSeparator() . $myId . $GLOBALS["myLogHandler"]->getSeparator() . "insert\n";
            $this->write_log_to_file();
        }
        }
        
        

          I shall be frank and say that I dont know if one should use function_exists(), method_exists(), or simply solve your problem by a different way.
          Also, I currently dont have the facility to test PHP scripts, so I cant find out.

          This is what I suggest you think over:
          1. Do you really need to use function_exists()?
          Basically, IMO, you want to know if the object is instantiated from the Person class, or from the BaseBean class.

          1. When using method_exists(), how did you use it?
            PHP Manual (user notes) gives method_exists($this, 'function_name') as a possible example, but $this would point to the parent, not the child, and hence always return false.
            Is there any other way?

            Basically, IMO, you want to know if the object is instantiated from the Person class, or from the BaseBean class.

            thats not the point, i just wanna know if this mehtod is declined in the object that calls the insert function

            but it was my fault, method_exists works, with correct parameters ;-)
            in this case (using extends) $this IS the person object, so method_exists returns true

            so, my problem is solved, thx a lot
            but i just dont know why function_exists doesnt work the way it should...

              in this case (using extends) $this IS the person object, so method_exists returns true

              hmm... yeah, I think my reasoning was a little faulty here.

                Write a Reply...