Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP class method named print... not allowed?

Tags:

php

I am writing a Controller for Codeigniter with a method named "print". The name is important, because I would like to be able to access the page at "http://www.mysite.com/mycontroller/print".

However, I cannot do this because there is the syntax error:

Parse error: syntax error, unexpected T_PRINT, expecting T_STRING

Is there any special syntax to make the PHP interpreter "think" that when I say:

class MyClass extends Controller {
    function print() {
        // print method here
    }
}

... that I am talking about a method with a T_STRING name of "print" and not the T_PRINT that it is expecting it to be?

like image 383
chaimp Avatar asked Mar 05 '10 16:03

chaimp


People also ask

What does :: class do in PHP?

::class ¶ The class keyword is also used for class name resolution. To obtain the fully qualified name of a class ClassName use ClassName::class . This is particularly useful with namespaced classes.

How do you call a function outside the class in PHP?

you can use require_once() with exact path of that class after that you should use the object of the included class and function name for calling the function of outer class. Save this answer.

What is the correct way of initiating a PHP class?

PHP: Creating classes and Instantiation The class definition starts with the keyword class followed by a class name, then followed by a set of curly braces ({}) which enclose constants, variables (called "properties"), and functions (called "methods") belonging to the class.

Why is it not recommended to make all of a class variable public in PHP?

For one thing, all such properties become public properties (and it's better to keep properties private in many cases for the purposes of encapsulation). It also makes the code in the class much less readable if new properties are being declared all over the place.


2 Answers

This won't work because print is a language construct in php. I know it looks like a function, but it is treated the same way as (for example) function, while, include, and so on.

like image 107
ryeguy Avatar answered Sep 28 '22 08:09

ryeguy


I found that you can get around the naming restriction for some reserved words in PHP 5.3 using the __call magic method.

public function __call( $name, $arguments )
{
  $fn = 'do_' . $name;
  if ( method_exists( $this, $fn ) )
  {
    return call_user_func_array( array( $this, fn), $arguments );
  }
  else
  {
    throw new Exception( 'Method "' . $name . '" does not exist.' );
  }
}

protected function do_print( /* $arg1, $arg2,...*/ )
{
  //whatever
}

I'm not saying this is necessarily a good idea, but it does allow you to make a call like $foo->print().

I really see no reason why reserved words can't be used as method names for classes. I haven't been able to come up with an ambiguous case that would need to be solved.

like image 31
zzzzBov Avatar answered Sep 28 '22 08:09

zzzzBov