Class returning object confusion - php

I've been asked to create a class that does some stuff, and then returns an object with read only properties.. Now I've created the class and I've got everything working 100%, however I'm confused when they say to 'return an object with read only properties'.
This is the outline of my php file which contains the class and some extra lines calling it etc:
class Book(){
protected $self = array();
function __construct{
//do processing and build the array
}
function getAttributes(){
return $this->self; //return the protected array (for reading)
}
}
$book = new Book();
print_r($book->getAttributes());
How can I return an object or something?

You are probably looking for the keyword final. Final means the object/method cannot be overridden.
Protected means that the object/method can only be accessed by the class who it belongs to.
Since self is a reserved keyword, you need to change that as well as your declarations. Rename $self an $this->self to $data and $this->data

self is a PHP reserved word. You have to renamed your variable.

What they're referring to is an object with private or protected properties which can only be accessed by setters/getters. The property will be read-only if you only define the getter method.

Something like:
Class Book {
protected $attribute;
protected $another_attribute;
public function get_attribute(){
return $this->attribute;
}
public function get_another_attribute() {
return $this->another_attribute;
}
public method get_this_book() {
return $this;
}
}
Now this is kind of s silly example because Book->get_this_book() would return itself. But this should give you an idea of how to set of getters on protected properties such that they are read only. And how to reutrn an object (in this case it returns itself).

read only property means you can access them but can not write them
class PropertyInaccessible {
//put your code here
protected $_data = array();
public function __get($name) {
if(isset ($this->_data[$name]))
return $this->_data[$name];
}
public function __set($name, $value) {
throw new Exception('Can not set property directly');
}
public function set($name, $value) {
$this->_data[$name] = $value;
}
}

Related

Using -> on a static object

While reading this: http://propelorm.org/ I noticed that they are using -> sign on a static object and using several different methods of it as a "one-liner".
Or does the static object return an instance which from the methods are being called from?
What ever is the case I would like to read more about this. What is this called and does it really work with static objects?
I noticed that they are using -> sign on a static object
By static object you mean to say static method and that the code you are referring to is this:
classBookQuery::create()->findPK(123);
They are not using -> on a static method. They are using -> on the object returned by the static method.
You can try it yourself:
class AClass
{
public static function aMethod()
{
return new BClass();
}
}
class BClass
{
public function bMethod($int)
{
echo $int;
}
}
AClass::aMethod()->bMethod(123); // output: 123
As others have mentioned in the comments, the static function BookQuery::create() just returns another object.
The "arrow" operates on the returned object.
Example:
class Car {
public function beep() {
echo 'BEEP';
}
}
class CarProvider {
public static function get() {
return new Car();
}
}
// outputs 'BEEP';
CarProvider::get()->beep();
// the same as this
$car = CarProvider::get();
$car->beep();
This is oftentimes know as method chaining. The static method call is returning an instantiated object against which another method is called. This sort of construct is often achieve like this:
class myobject_factory {
public static function get_object () {
return new myobject;
}
}
class myobject {
protected $some_property;
public function set_some_property($value) {
$this->some_property = $value;
return $this;
}
public function get_some_propert() {
return $this->some_property;
}
}
$property = myobject_factory::get_object()->set_some_property('foobar')->get_some_property();
This is obviously a trivial example, but you can see how chaining is enabled because the setter on myobject returns $this.

PHP how to use magic method with static class? [duplicate]

This question already has answers here:
Magic __get getter for static properties in PHP
(6 answers)
Closed 9 years ago.
I'm trying to convert array to object. I want to use magic methods - __get and __set with static properties.
My code:
class UserData {
private static $id, $name, $login;
public function __get($var)
{
return self::$var;
}
public function __set($var, $val)
{
self::{$var} = $val;
}
}
And setting:
foreach($userArray as $key => $val)
{
DaneBilingowe::${$key} = $val;
}
Error:
Fatal error: Cannot access private property UserData::$id
Is it possible to use magic method with static property?
In short, no.
__get() and __set() are instance methods. They are essentially the functions that make up the stdClass(), which is an instance.
If you must set static content in this manner you can give the class a stdClass parameter and a singleton structure that would allow you to magically set and get data.
For example:
class UserData {
protected static $_instance;
protected $_data = array();
public static function get_instance() {
static $initialized = FALSE;
if ( ! $initialized) {
self::$_instance = new UserData;
$initialized = TRUE;
}
return self::$_instance;
}
public function __get($var) {
$self = self::get_instance();
return isset($self->_data[$var]) ? $self->_data[$var] : NULL;
}
public function __set($var, $val) {
$self = self::get_instance();
$self->_data[$var] = $val;
}
}
Then you could go:
$UserData =& UserData::get_instance();
$UserData->var = 'val';
echo $UserData->var; // prints 'val'
I don't recommend using Singletons in PHP, however, because they are pointless. You can read some reasons why in the post Best practice on PHP singleton classes.
Either use a static class or an instance class.
Magic getters and setters are shortcuts. You can implement the same behavior with normal setters and getters. The example below provides the same functionality, but the intent is a lot more clear:
class UserData {
protected $id, $name, $login;
public static function set_name($name) {
self::$name = $name;
}
public static function set_login($login) {
self::$login = $login;
}
public static function get_id() {
return self::$id;
}
public static function get_name() {
return self::$name;
}
public static function get_login() {
return self::login;
}
}
Notice how in the above code $id is not writable. It is only readable. $name and $login are readable and writable. It is easier and less buggy to control reading and writing using normal setters and getters. Magic methods are just that, magic, and usually magic is not concrete and is less understandable in code.
The final point I want to make is, why would UserData be static? Unless you only have 1 user in the entirety of your code it doesn't make sense to have it static. Perhaps I am not getting the whole picture, but something with an id and name should be instantiated so that you can have multiple instances. Otherwise, why have the id because the class itself is unique.
If you really want to use magic methods on static properties, you can but you will need an instance. Though it does not look reasonable, being a programmer itself is not reasonable at all :)
Also user defined classes and objects are not dynamic in php.
You can not add variables to them that easily... So you can use the pattern below:
class UserData {
private static $id, $name, $login, $arr = [];
public function __get($var){
return (array_key_exists(self::$arr, $var)? self::$arr[$var]:null;
}
public function __set($var, $val){
self::$arr[$var] = $val;
}
}
And setting: Well what is DaneBilingowe? I do not now here... But:
$inst = new UserData();
foreach($userArray as $key => $val){
$inst->$key = $val;
}
will work.
But beware, It will work only on class (static) memory.
Also since there is no appropriate filtering for setting names, weird things can happen.
(That means you should add them)

Not able to access private protect member in iterator php

Here is the data Iterator implementation
//Data Iterator
class DataIterator implements Iterator
{
public $data ;
public function __construct(Data $obj)
{
$this->data = $obj;
}
public function rewind()
{
$this->properties = get_object_vars($this->data);
}
public function valid()
{
if (key($this->properties) === null )
{
return false;
}
return true;
}
public function key()
{
return key($this->properties);
}
public function current()
{
return current($this->properties);
}
public function next()
{
next($this->properties);
}
}
and here is data class
/*Data Class*/
class Data implements IteratorAggregate
{
public $name;
private $age;
protected $address;
public $country;
public $state;
public function __construct($name, $age, $address, $country = 'USA', $state = 'NH')
{
$this->name = $name;
$this->age = $age;
$this->address = $address;
$this->country = $country;
$this->state = $state;
}
function getIterator()
{
return new DataIterator($this);
}
}
And here is the calling part
$data = new Data('Joker', '27', 'California');
foreach($data->getIterator() as $key => $value)
{
echo $key , ' ', $value, '<br>';
}
output
name Joker
country USA
state NH
Notice that the output does not contain my private and protected properties (age, address) output.
How do I tell Iterator to output those as well?
You cannot tell the iterator to output those properties because they are simply not accessible from the outside (i.e. the point where the iterator does get_object_vars($this->data).
There are two ways you could go about doing this:
By having the data object pass the values to the iterator.
Use the reflection API to pull them out by force (verbose, slow!).
But before going ahead with #1 as the preferred option, stop for a moment and ask yourself: why does the iterator expose non-public members of the data object?
Making something private means "You people don't really need to know about this; it may go away in the future, or it may change beyond recognition". If it's something that the outside world cares about, then why is it not public (either directly, or exposed through a public getter)? A rethink of what this iterator's purpose is might be in order.
That said, here's how you would do #1:
class DataIterator implements Iterator
{
public $data;
private $properties;
public function __construct(Data $obj, array $propeties)
{
$this->data = $obj;
$this->properties = $properties;
}
public function rewind()
{
// Arguably horrible trick to refresh the property map without
// demanding that Data exposes a separate API just for this purpose
$newIterator = $this->data->getIterator();
$this->properties = $newIterator->properties;
}
}
class Data implements IteratorAggregate
{
function getIterator()
{
return new DataIterator($this, get_object_vars($this));
}
}
Public, private and protected are access modifiers. They are designed to restrict the accessibility of your class attributes.
Public means that any one can access that attribute, so if someone wants, they can change the value, without that you know it.
Private mean that the attribute is only accessible INSIDE the class,
so nobody can "mess" with those properties from OUTSIDE the class.
Protected is similar like Private, but child classes (classes that
inherit from that class) have access to it.
You are making age and address private, so you are basically saying, nobody is allowed to access these attributes. If you want to access private/protected attributes, you will have to make getters and setters and call these functions, or make the attributes public.
try get_class_vars
$this->properties = get_class_vars(get_class($this->data));
instead of
$this->properties = get_object_vars($this->data);

PHP: Detect instances of same class

I have a PHP Class which requires a unique value in its constructor. If multiple instances of the same class are passed the same value the results are horrific.
How would I go about detecting other instances of a Class so I can check and prevent this from happening before constructing any new ones?
A simple solution would be to keep a static array of the values inside the class. Then, when a new instance is created, check the static array's contents in the constructor.
Something like..
class Foo {
private static $usedValues = array();
public function __construct($val) {
if(in_array($val, self::$usedValues)) {
throw new Exception('blah');
}
self::$usedValues[] = $val;
}
}
I think the multiton pattern is right for you.
class Foo {
static protected $_instances = array();
static public function getInstance($id) {
if(!self::exists($id)) {
self::$_instances[$id] = new Foo($id);
}
return self::$_instances[$id];
}
static public function exists($id) {
return isset(self::$_instances[$id]);
}
protected function __construct($id) {
}
}

PHP make class propery inheritable

i have 2 classes A and B that extends A. A has a public property and i want that on the instances of B that property can't be accessible. I try to explain better:
class A
{
public $prop;
}
class B extends A
{
...
}
$instance=new B;
$instance->prop; //This must throw an error like "Undefined property prop"
I tried with the final keyword but it's only available for methods not for properties. I tried also by setting the same property as private on B but PHP does not allow to change the access level from public to private or protected.
Maybe there's a simple solution to this problem but i can't find it so do you know a way to do this?
Simply change public $prop; to private $prop; by making $prop public you are making it accessible by every possible way, but making it private will make it accessible within a class only
Use magic methods
class A {
protected $_prop;
public function __get($key) {
if ($key=='prop') {
return $this->_prop;
}
}
public function __set($key, $value) {
if ($key=='prop') {
return $this->_prop = $value;
}
}
}
class B extends A {
public function __get($key) {
}
public function __set($key, $value) {
}
// how you can use it
public function foo() {
echo $this->_prop;
}
}
Hmm. Tricky!
The only idea that comes to mind is making $prop private from the start, and using magic getter and setter functions to control access. You'd have to store the access information in a separate array and then either return the correct value, or throw a fatal error in the getter function.

Categories