Jump to content

[SOLVED] Overriding functions in classes


Jessica

Recommended Posts

Why can I override certain functions, but not others?

Example: (not the real code)
[code]<?php
Class myClass{
  var $str
 
  function extract(){
      return $this->str;
  }

  function print(){
    return $this->str;
  }
}
?>[/code]

It's fine with extract(), but when I add in the print() one, I get a parse error saying unexpected print. Why can I do this with extract but not print?
Link to comment
https://forums.phpfreaks.com/topic/36415-solved-overriding-functions-in-classes/
Share on other sites

You're mising a semi-colon after var $str

Extract() is a reserved function, while print() is reserved too but it's a language construct (can be used without parenthesis).

It's usually a bad idea to use variable or function names that are the same or similar to PHP reserved words.

hth.
Thx Hypnos. As I mentioned, it's a bad idea to use PHP resrved words for pretty much anything.

Same goes for MySQL - some people like to use a column name of password but it's reserved (you should use a different name or enclose it in backtick marks `password` within SQL and stored procedures).
Yeah every now and then I accidentally use a reserved MySQL word without knowing, and after a few minutes of trying to debug a query that looks fine, I always check the list.
I looked up functions, etc in the manual, but it didn't occur to me that print was a reserved PHP word. :D

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.