3 Replies - 1542 Views - Last Post: 12 September 2012 - 02:19 PM Rate Topic: -----

#1 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 236
  • Joined: 28-March 12

[QUESTION] $this->alias?

Posted 12 September 2012 - 01:32 PM

Can someone tell me what does $this->alias do or mean?? It would be even better if someone can point me to some documentation on this because I can't seem to find it. Today I was going through someone project and came across a statement with $this->alias in it, but I don't really understand what it is doing! Any light on this subject would be greatly appreciated! Thanks in advance!
Is This A Good Question/Topic? 0
  • +

Replies To: [QUESTION] $this->alias?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3273
  • Posts: 11,559
  • Joined: 08-August 08

Re: [QUESTION] $this->alias?

Posted 12 September 2012 - 02:12 PM


It's OOP. The object has a variable called alias that is being accessed.
Try the code below and see if you can figure out how it's doing what it does. If you can, then try adding circles or triangles to the mix. Hint: you'll want to read up on inheritance.

class box {
	protected $top;	
	protected $left;
	protected $width;
	protected $height;
	protected $box_color;
	protected $image;
	function __construct(&$im) {
		$this->image = $im;
		$this->top = rand(1,550);
		$this->left = rand(1,550);
		$this->width = rand(2,100);
		$this->height = rand(2,100 );
		$this->box_color = imagecolorallocate($im, rand(0,240), rand(0,240), rand(0,240));

	function draw_box() {
		imagefilledrectangle($this->image, $this->left, $this->top, $this->left + $this->width, $this->top + $this->height, $this->box_color);


$im = imagecreate(600, 600) or die("Cannot Initialize new GD image stream");
$background = imagecolorallocate($im, 240, 240, 200);
$boxes = array();
$n = rand(10,100);
for($i = 0; $i < $n; $i++) {
	$boxes[$i] = new box($im);

echo '<img src="testing.png" />';

Was This Post Helpful? 6
  • +
  • -

#3 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 3858
  • View blog
  • Posts: 6,364
  • Joined: 08-June 10

Re: [QUESTION] $this->alias?

Posted 12 September 2012 - 02:13 PM


It references a class property called "alias" in the current object. The $this variable always represents the object it is used in, and the $this->name syntax accesses the the property called "name" in that object.

You can read all about Classes and Objects in the manual. More specifically, you can read the part about Properties to get more info on this specific syntax.
Was This Post Helpful? 6
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 823
  • View blog
  • Posts: 1,755
  • Joined: 30-January 09

Re: [QUESTION] $this->alias?

Posted 12 September 2012 - 02:19 PM


In PHP, that's how you refer to a property of an instantiated object. A property can be considered to be a variable that is encapsulated by it's class, and can be declared as public, protected or private. Public properties can be accessed by direct reference from anywhere in your code (e.g. $myObject->myProperty). Protected properties can be accessed from within the class, or any class that inherits from that class. Private properties can be accessed by that class, and that class alone.

Here is an example:
// Declare the class in PHP
class Foo
  // ...and give it a public property
  public $bar = 'Hello World';

// Now, we create an "instance" of the class, or in other words, an object
// We can also call this "instantiating an object"
// This creates a working copy of the class that is distinct from any other object instantiated from the Foo class
$myClass = new Foo();
echo $myClass->bar;

The $this part of that code snippet you provided is how you refer to a property from within an object. Here is an example:
class Foo2
  private $bar = 'Hello World again';

  public function HelloWorld()
    return $this->bar;

$myClass = new Foo2();
echo $myClass->HelloWorld();

You'll notice in the above code that I have made the property private. That means if we try this:
echo $myClass->bar;

...an error will be thrown. We grant access to the private property by creating a "method", which is a function that is encapsulated by it's class. In a nutshell:


Variables are to properties, as functions are to methods

These are a couple of the underlying principles to OOP (Object Oriented Programming). While OOP is not necessary in PHP, it is a good choice that is well worth studying up on, as it can reduce your coding time drastically, especially with large applications. Here's a link to the manual page on Classes and Objects, which has relevant links to the various aspects of OOP in PHP.

If you are looking to learn OOP, reading the Wikipedia page, while a bit to take in, will indeed help with grasping the core concepts.
Was This Post Helpful? 5
  • +
  • -

Page 1 of 1