views:

58

answers:

3

I am trying to assign a variable to a class in PHP, however I am not getting any results?

Can anyone offer any assistance? The code is provided below. I am trying to echo the URL as shown below, by first assigning it to a class variable.

class PageClass {


    var $absolute_path = NULL;

    function get_absolute_path(){

    $url = $this->absolute_path;

    echo $url;

    }

}

$page = new PageClass();
$page->absolute_path = "http://localhost:8888/smile2/organic/";

$page->get_absolute_path(); //this should echo the URL as defined above - but does not
+3  A: 
class PageClass {

    public $absolute_path = NULL;

    function get_absolute_path(){
       $url = $this->absolute_path;
       return $url;
    }

}

$page = new PageClass();
$page->absolute_path = "http://localhost:8888/smile2/organic/";

echo $page->get_absolute_path(); 
Alexander.Plutov
+1  A: 

Works fine for me.
Have you checked that the script and esp. the code in question is executed at all?
E.g. add some unconditional debug-output to the script. Or install a debugger like XDebug to step through the code and inspect variables.

<?php
class PageClass {
  var $absolute_path = NULL; // old php4 declaration, see http://docs.php.net/oop5

  function get_absolute_path() { // again old php4 declaration
    $url = $this->absolute_path;
    echo "debug: "; var_dump($url);
    echo $url;
  }
}

$page = new PageClass();
$page->absolute_path = "http://localhost:8888/smile2/organic/";

echo "debug: page->get_absolute_path\n";
$page->get_absolute_path();
VolkerK
+3  A: 

It also works for me.

Take a look at a live example of your code here.

However, there are a few things you should change about your class.

First, Garvey does make a good point that you should not be using var. That's the older PHP4, less OOP conscious version. Rather declare each variable public or private. In fact, you should declare each function public or private too.

Generally, most classes have private variables, since you usually only want to change the variables in specific ways. To achieve this control you usually set several public methods to allow client functions to interact with your class only in restricted predetermined ways.

If you have a getter, you'd probably want a setter, since these are usually used with private variables, like I described above.

A final note is that functions named get usually return a value. If you want to display a value, it is customary to use a name like display_path or show_path:

<?php

   class PageClass 
   { 
        private $absolute_path = NULL;

        public function set_absolute_path($path)
        {
            $this->absolute_path = $path;   
        }

        public function display_absolute_path()
        {
            echo $this->absolute_path;
        }
    }

$page = new PageClass();
$page->set_absolute_path("http://localhost:8888/smile2/organic/");

$page->display_absolute_path();
  // The above outputs: http://localhost:8888/smile2/organic/

// Your variable is now safe from meddling.
// This:
// echo $this->absolute_path;
// Will not work. It will create an error like:
// Fatal error: Cannot access private property PageClass::$absolute_path on ...

?>

Live Example Here

There's a section on classes and objects in the online PHP reference.

Peter Ajtai