Jump to content

Me -> new_to_oop(); Can't declare new variable in a method?


br3nn4n

Recommended Posts

Here's my code:

 

<?php
  function query($query) {
   $q = mysql_query($query) or die("Error, please try later");
   $q = mysql_fetch_array($q);
   return $q;
  }
?>

 

And I'm getting:

 

Parse error: syntax error, unexpected T_VAR in ...

 

As I said I'm really new to OOP (in general) and classes in PHP, so I'm just missing something simple. Any help :D

Here's the right code:

 

<?php

class admin {
  public $page;

  function admin($page) {
   $this -> page = $page;
   $this->db_connect();
  }

  function db_connect() {
   mysql_connect('......stripped....');
   mysql_select_db("wri");
   mysql_query("SET NAMES 'utf8'");
   return $this;
  }

  function query($query) {
   var $q;
   $q = mysql_query($query) or die("Error, please try later");
   $q = mysql_fetch_array($q);
   return $q;
  }

   function title() {
    // some crazy querying shit here...
    $title = ucfirst($this->page);
    return $title;
   }

  function page() {
  $this->query("SELECT * FROM pages WHERE page='".$this->page."' AND administrative='1'");
  $this->content = print_r($this->q);
  return $this;
  }

  function content() {
  return $this->content;
  }

} //site class

?>

 

Thanks!

What version of php are you running? perhaps php 4?

I dont get this error "Parse error: syntax error, unexpected T_VAR in ..."

 

I do see something wrong in the following

function query($query) {
   var $q;
   $q = mysql_query($query) or die("Error, please try later");
   $q = mysql_fetch_array($q);
   return $q;
  }

The var infront of the $q shouldnt be inside a function remove var here

Here's a PHP5 valid version of your code with PHPDoc comments:

 

<?php
/**
* Admin Class
* @author br3nn4n
*/
class admin {

  /**
   * Name of the page.
   * @var string
   */ 
  private $_page;


  /**
   * String containing the page content.
   * @var string
   */
  private $_content;

  /**
   * Constructor
   * @param string $page
   */
  function __construct($page) {
   $this->setPageName($page)
        ->initDB();
  }

  /**
   * Initialise the database connection.
   */
  protected function initDB() {
   mysql_connect('......stripped....');
   mysql_select_db("wri");
   mysql_query("SET NAMES 'utf8'");
   return $this;
  }

  /** 
   * Set the internal page name.
   * @param string $page
   * @return admin - Allows chaining of methods.
   */
  public function setPageName($page){
    $this->_page = $page;
    return $this;
  }

  /**
   * Execute SQL query and return 1st row as numeric indexed array.
   * @param string $query
   * @return array
   */
  public function query($query) {
   $q = mysql_query($query) or die("Error, please try later");
   $q = mysql_fetch_array($q);
   return $q;
  }

   /**
    * Get the title based on page name.
    * @return string
    */
   public function title() {
    // some crazy querying shit here...
    $title = ucfirst($this->getPageName());
    return $title;
   }

  /**
   * Retrieve the name of the page.
   * @return string
   */
  protected function getPageName(){
    return $this->_page;
  }

  /**
   * Calling page() loads the content from the DB internally.
   * @return admin - Allows method chaining.
   */
  public function function page() {
   $page = $this->query("SELECT * FROM pages WHERE page='{$this->getPageName()}' AND administrative='1'");
   // This is a guess but i'm loading the 1st part of the return query array as the content.
   $this->_content = $page[0];
   return $this;
  }

  /**
   * Get the "page" content.
   * @return string
   */
  public function content() {
    if(!$this->_content){
      $this->page();
    }
    return $this->_content;
  }

}

/**
* @example
*/
$admin = new admin();
print $admin->content();


?>

Alright...! haha, like I admitted I'm still struggling to understand OOP so your example (as simple as it is) has me sort of confused.

 

To clarify: I know about chaining (and returning $this..), that makes sense. But what about the underscores before variables? Not talking about the double underscore to declare the constructor, that's something I get also. But why not just use $page, instead of $_page?

 

Sorry if I'm being a bug :]

  Quote

But what about the underscores before variables? Not talking about the double underscore to declare the constructor, that's something I get also. But why not just use $page, instead of $_page?

 

Anyone correct me if i am wrong.

 

I think it's a style preference to make the difference more clear between public variables and protected + private vars.

 

You don't have to use the underscores it will work fine without it.

  Quote

  Quote

But what about the underscores before variables? Not talking about the double underscore to declare the constructor, that's something I get also. But why not just use $page, instead of $_page?

 

Anyone correct me if i am wrong.

 

I think it's a style preference to make the difference more clear between public variables and protected + private vars.

 

You don't have to use the underscores it will work fine without it.

 

Alright, that was sort of what I thought (just a formality / style thing).

 

@aschk: And I plugged in your code and got this:

 

Warning: Missing argument 1 for admin::__construct(), called in ...../admin_class.php on line 106 and defined in ...../admin_class.php on line 25

Notice: Undefined variable: page in ...../admin_class.php on line 26
Error, please try later

 

And I'm sure I'm instantiating the object with the variable, so what could be wrong? I fixed your minor double "function" already, that was a separate error but it's gone now :D

 

Thank you much!

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.