Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

In OOP:

private - you can access that property only from inside the class

class Private
{
	private $foo;
	
	function setFoo($foo)
	{
		$this->foo = $foo;
	}
	
	function getFoo()
	{
		return $this->$foo
	}
}

$privateObj = new Private;

$privateObj->foo = 'bar'; // Returns error
echo $privateObj->foo; // Returns error

$privateObj->setFoo('bar') // Sets the $foo parameter = 'bar';
echo $privateObj->getFoo() // Returns 'bar';

public - you can access that property from an object

class Public
{
	public $foo;
	
	function setFoo($foo)
	{
		$this->foo = $foo;
	}
	
	function getFoo()
	{
		return $this->$foo
	}
}


$publicObj = new Public;

$publicObj->foo = 'bar'; // Sets the $foo parameter = 'bar';
echo $publicObj->foo; // Returns 'bar';

$publicObj->setFoo('bar') // Sets the $foo parameter = 'bar';
echo $publicObj->getFoo() // Returns 'bar';

protected - you can access that property from inside the class and from inside of class's children

class Protected
{
	protected $foo;
		
	function setFoo($foo)
	{
		$this->foo = $foo;
	}
	
	function getFoo()
	{
		return $this->$foo
	}
}


class ProtectedChild extends Protected
{
	
}


$protectedChildObj = new ProtectedChild;

$protectedChildObj->foo = 'bar'; // Returns error
echo $protectedChildObj->foo; // Returns error;

$protectedChildObj->setFoo('bar') // Sets the $foo parameter = 'bar';
echo $protectedChildObj->getFoo() // Returns 'bar';

Hope this helps

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ottz0 ottz0 Joined 15 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.