Jump to content

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


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 :]

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.

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.