CodeIgniter Forums
PHP5 OOP Standards - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: PHP5 OOP Standards (/showthread.php?tid=19770)

Pages: 1 2


PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]IamPrototype[/eluser]
Hi guys! Smile

I've read a lot about PHP5 standards, but I was wondering if there were any - special - OOP standard in PHP5?

Btw, what is the "right" why to declare variables in the start of a class? I've seen a guy doing this.

Code:
public $name = "John Doe";

Step away from public... and tell me if that's a way to do it or a wrong way to do it?

I like doing this (mostly because I've seen a lot people are doing it like this).

Code:
public var $name = "John Doe";

Any links with PHP5 OOP standards would be nice! Thanks! Smile


PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]Phil Sturgeon[/eluser]
var is just to tell PHP that this is a variable (more accurately a property) of your class.

public/private/protevted tell PHP 5 that it is a special kind of var. This means you dont need to say that it is a "public variable" and then also mention it is a "variable" as you are repeating yourself.


PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]IamPrototype[/eluser]
I know what public, private and protected does. Just thought it was better not to write "var". Oh well, I like it that way and I'll keep it that way, I think it's more organized and well structured.


PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]Yorick Peterse[/eluser]
Public stuff : Can be used outside the class itself

Code:
<?php
class class_1 {
  public var $foo = "foo";
}

$class = new class_1();
echo $class->foo; // Prints foo

?>

Private stuff : Can only be used in the class itself

Code:
<?php
class class_1 {
  private var $foo = "foo";
}

$class = new class_1();
echo $class->foo; // Returns an error

?>

Protected : Can be used outside the class, but with limitations

Code:
<?php
class class_1 {
  private var $foo = "foo";
}

// This will not work
$class = new chass_1();
echo $class->foo;

// However this will
class class_2 extends class_1 {
  function foo() {
    echo $this->foo; // Prints foo
  }
}

?>



PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]xwero[/eluser]
var is kept in php5 and php6 as an alias for public.


PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]Dam1an[/eluser]
I've never seen anyone write public/protected/private and var
I stick to the PHP5 syntax for declaring variables/functioncs, cause I prefer it, and force of habit (damn Java)


PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]Yorick Peterse[/eluser]
[quote author="Dam1an" date="1245351368"]I've never seen anyone write public/protected/private and var
I stick to the PHP5 syntax for declaring variables/functioncs, cause I prefer it, and force of habit (damn Java)[/quote]

I actually never use it either, but it doesn't really make a difference Smile


PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]sl3dg3hamm3r[/eluser]
[quote author="Dam1an" date="1245351368"]I've never seen anyone write public/protected/private[/quote]

?
I would highly suggest that for the sake of proper OOP design...


PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]n0xie[/eluser]
Dam1an meant to say he never saw the combination of both public/private/protected AND var.

In PHP4 you write:
Code:
var $myvar;

in PHP5 you write:
Code:
public $myvar;

Using BOTH is just plain weird:
Code:
public var $myvar;



PHP5 OOP Standards - El Forum - 06-18-2009

[eluser]TheFuzzy0ne[/eluser]
It makes little or no difference to the way it's parsed, so it's purely for aesthetics. You can easily tell what follows a public/private/protected keyword by the prefix. If it starts with a '$' then you know it's a variable, otherwise it's a method. That's enough for me, so I don't bother with using var.