Why does PHP have both "resource" data type and "object" data type? - php

It seems that PHP have two data types for objects: "resource" and "object".
For example the following object is a "resource":
$file = fopen("1.txt", "w");
// will print: "resource"
echo gettype($file);
And the following object is an "object":
$db = mysqli_connect();
// will print: "object"
echo gettype($db);
But why there are two data types for objects and not just one data type?

Resources are not objects! They are a distinct data type.
The reasons for this are purely historical. PHP didn't have any object-oriented capabilities at the start. The language wasn't meant to be a new language but rather a templating engine for C API. C doesn't have classes.
PHP needed a way to represent a pointer to internal resources, e.g. opened files, database connections, or image canvas. PHP could just use integers to do this, but it would not be enough. The internal resources had to be refcounted and remembered by PHP. PHP, unlike C, has garbage collection and abstracts memory handling*. You can't do that with just an ID number. PHP had to return a variable that internally would point to a resource. It was decided that the simplest way to achieve this is to have a special resource data type. Nowadays, it is done with an object, e.g. CurlHandle.
Resources were a terrible idea. They do not play nicely with the rest of the type system. They are an exception that is confusing to many PHP users. Since PHP 7, the goal has been to migrate all resources to opaque objects and remove resource data type. While it's not gone yet, it can be considered a legacy and a piece of PHP history.
For more information about resource data type and how it works internally visit www.phpinternalsbook.com
* While most resources have a dedicated function to free them, this is unnecessary as PHP will close the handle and free up the memory once no more variables are pointing to it. The resource data type tracks where the handle is used and whether it's still needed.

Related

best format to store php object to postgresql database

I wonder what is the best format to store PHP objects to database, an object have multiple parameters and methods, XML or binary or other formats?
for example :
$me = new Person();
$me->code= '007';
$me->name='antoine';
$me->store(); //this object is now stored into postgresql in Binary format or XML or ... you tell me !
//after few days :
$object = $database->retrievePersonByCode('007');
echo "my name is".$object->name;
How to do that ?
You will need to serialize it first. PHP has a serialize() function which you can use. But you should pay attention to this when using it:
Object's private members have the class name prepended to the member
name; protected members have a '*' prepended to the member name. These
prepended values have null bytes on either side.
Depending on the size of the object you wish to serialize you can use TEXT, MEDIUMTEXT or even LONGTEXT.
Personally I've standardized on json since any language can read it back easily. Plus it's human readable (not binary) and not nearly as verbose as XML. Since Postgres actually supports json as a data type, that is an additional bonus.
This question is the first stone of the existence of ORM and object oriented persistence layers.
Either you store PHP objects as is in a serialized way (text, json, xml, whatever...) and you loose the ability to perform queries on a particular attribute of your objects (ie looking for all Personn objects younger than 18 by example). Changing one atribute in your instance means updating the whole object.
Or you use a code layer that transposes your PHP objects to real tables or sets in the database. There is the ORM familly that uses abstraction layer (Doctrine, Propel and thousands more...) and there is the OMM familly that just uses Postgres (Pomm).

Bridge between JS and PHP complex data

I am required to build a complex control panel.
It has to be user configured and after he has made an arbitrary number of changes, he submits them and is presented with the newly configured report.
Now there are a lot of possible configuration options and the JS must be able to draw the control elements/init them with values and PHP must do the business logic - all based on given configuration object - and both must be able to change the values of options before passing the object to the other side.
Now the obvious solution is to have predefined configuration objects on both sides:
PHP:
class config {
public $anArray = array();
}
JS:
{
anArray : []
}
And the sides can communicate fluently via json_encode and $.parseJSON.
However, the structure of both entities must match and I've no idea how to ensure that. Has anyone any ideas on how to communicate complex data structures among those two technologies?
EDIT: to clarify, by structure I mean the properties of the two objects: the object itself is a simple configuration wrapper so it has no private properties, no methods etc, only public fields that are scalar or simple indexed arrays.
So I want a solution to develop it in a DRY way: if I want to add a field called for example "paginationEnabled" it should appear in both - the JS and PHP object:
PHP:
class config {
public $anArray = array();
public $paginationEnabled = true;
}
JS:
{
anArray : [],
paginationEnabled : true
}
I could implement them separately on both sides, but it would not be DRY (which is terrible in this scenario) and I can sense there must be a solution for that, I just can't think of one.
The simplest solution is to included the entire JSON-encoded object structure on every request to the server, and in every page returned by the server. This will be necessary anyway since neither PHP nor JavaScript persists between page loads (unless you're using AJAX, which would only make the JavaScript persist anyway).
Of course that only works if the data is already in a form that can be JSON-encoded. (Arrays and simple objects can be encoded to any depth, but not objects of nontrivial classes, file handles or functions.) If it isn't, then you'll need to convert it to one, then convert back on the other end - for example, if the class of an object is important, then you first need to ensure that a corresponding class exists on the other side, then you can indicate the desired classname with {class: $class, value: $value} - and add code on the other end to find that construction and rebuild an object of the desired class. If you need to copy functions across, then that might be manageable if you store all of your functions as JavaScript, and include a JavaScript interpreter library in PHP. That's probably more trouble than it's worth, however.
If the JSON object is much too large to send on every request, then you're left with storing it in the database (in a form suitable for databases) and using AJAX to update the database when something changes on the JavaScript side, or to retrieve any part of the object that the client code doesn't already have.

Why return object instead of array?

I do a lot of work in WordPress, and I've noticed that far more functions return objects than arrays. Database results are returned as objects unless you specifically ask for an array. Errors are returned as objects. Outside of WordPress, most APIs give you an object instead of an array.
My question is, why do they use objects instead of arrays? For the most part it doesn't matter too much, but in some cases I find objects harder to not only process but to wrap my head around. Is there a performance reason for using an object?
I'm a self-taught PHP programmer. I've got a liberal arts degree. So forgive me if I'm missing a fundamental aspect of computer science. ;)
These are the reasons why I prefer objects in general:
Objects not only contain data but also functionality.
Objects have (in most cases) a predefined structure. This is very useful for API design. Furthermore, you can set properties as public, protected, or private.
objects better fit object oriented development.
In most IDE's auto-completion only works for objects.
Here is something to read:
Object Vs. Array in PHP
PHP stdClass: Storing Data in an Object Instead of an Array
When should I use stdClass and when should I use an array in php5 oo code
PHP Objects vs Arrays
Mysql results in PHP - arrays or objects?
PHP objects vs arrays performance myth
A Set of Objects in PHP: Arrays vs. SplObjectStorage
Better Object-Oriented Arrays
This probably isn't something you are going to deeply understand until you have worked on a large software project for several years. Many fresh computer science majors will give you an answer with all the right words (encapsulation, functionality with data, and maintainability) but few will really understand why all that stuff is good to have.
Let's run through a few examples.
If arrays were returned, then either all of the values need to be computed up front or lots of little values need to be returned with which you can build the more complex values from.
Think about an API method that returns a list of WordPress posts. These posts all have authors, authors have names, e-mail address, maybe even profiles with their biographies.
If you are returning all of the posts in an array, you'll either have to limit yourself to returning an array of post IDs:
[233, 41, 204, 111]
or returning a massive array that looks something like:
[ title: 'somePost', body: 'blah blah', 'author': ['name': 'billy', 'email': 'bill#bill.com', 'profile': ['interests': ['interest1', 'interest2', ...], 'bio': 'info...']] ]
[id: '2', .....]]
The first case of returning a list of IDs isn't very helpful to you because then you need to make an API call for each ID in order to get some information about that post.
The second case will pull way more information than you need 90% of the time and be doing way more work (especially if any of those fields is very complicated to build).
An object on the other hand can provide you with access to all the information you need, but not have actually pulled that information yet. Determining the values of fields can be done lazily (that is, when the value is needed and not beforehand) when using an object.
Arrays expose more data and capabilities than intended
Go back to the example of the massive array being returned. Now someone may likely build an application that iterates over each value inside the post array and prints it. If the API is updated to add just one extra element to that post array then the application code is going to break since it will be printing some new field that it probably shouldn't. If the order of items in the post array returned by the API changes, that will break the application code as well. So returning an array creates all sorts of possible dependencies that an object would not create.
Functionality
An object can hold information inside of it that will allow it to provide useful functionality to you. A post object, for instance, could be smart enough to return the previous or next posts. An array couldn't ever do that for you.
Flexibility
All of the benefits of objects mentioned above help to create a more flexible system.
My question is, why do they use objects instead of arrays?
Probably two reasons:
WordPress is quite old
arrays are faster and take less memory in most cases
easier to serialize
Is there a performance reason for using an object?
No. But a lot of good other reasons, for example:
you may store logic in the objects (methods, closures, etc.)
you may force object structure using an interface
better autocompletion in IDE
you don't get notices for not undefined array keys
in the end, you may easily convert any object to array
OOP != AOP :)
(For example, in Ruby, everything is an object. PHP was procedural/scripting language previously.)
WordPress (and a fair amount of other PHP applications) use objects rather than arrays, for conceptual, rather than technical reasons.
An object (even if just an instance of stdClass) is a representation of one thing. In WordPress that might be a post, a comment, or a user. An array on the other hand is a collection of things. (For example, a list of posts.)
Historically, PHP hasn't had great object support so arrays became quite powerful early on. (For example, the ability to have arbitrary keys rather than just being zero-indexed.) With the object support available in PHP 5, developers now have a choice between using arrays or objects as key-value stores. Personally, I prefer the WordPress approach as I like the syntactic difference between 'entities' and 'collections' that objects and arrays provide.
My question is, why do they (Wordpress) use objects instead of arrays?
That's really a good question and not easy to answer. I can only assume that it's common in Wordpress to use stdClass objects because they're using a database class that by default returns records as a stdClass object. They got used to it (8 years and more) and that's it. I don't think there is much more thought behind the simple fact.
syntactic sugar for associative arrays
-- Zeev Suraski about the standard object since PHP 3
stdClass objects are not really better than arrays. They are pretty much the same. That's for some historical reasons of the language as well as stdClass objects are really limited and actually are only sort of value objects in a very basic sense.
stdClass objects store values for their members like an array does per entry. And that's it.
Only PHP freaks are able to create stdClass objects with private members. There is not much benefit - if any - doing so.
stdClass objects do not have any methods/functions. So no use of that in Wordpress.
Compared with array, there are far less helpful functions to deal with a list or semi-structured data.
However, if you're used to arrays, just cast:
$array = (array) $object;
And you can access the data previously being an object, as an array. Or you like it the other way round:
$object = (object) $array;
Which will only drop invalid member names, like numbers. So take a little care. But I think you get the big picture: There is not much difference as long as it is about arrays and objects of stdClass.
Related:
Converting to object PHP Manual
Reserved Classes PHP Manual
What is stdClass in PHP?
The code looks cooler that way
Objects pass by reference
Objects are more strong typed then arrays, hence lees pron to errors (or give you a meaningful error message when you try to use un-existing member)
All the IDEs today have auto-complete, so when working with defined objects, the IDE does a lot for you and speeds up things
Easilly encapsulate logic and data in the same box, where with arrays, you store the data in the array, and then use a set of different function to process it.
Inheritance, If you would have a similar array with almost but not similar functionality, you would have to duplicate more code then if you are to do it with objects
Probably some more reason I have thought about
Objects are much more powerful than arrays can be.
Each object as an instance of a class can have functions attached.
If you have data that need processing then you need a function that does the processing.
With an array you would have to call that function on that array and therefore associate the logic yourself to the data.
With an object this association is already done and you don't have to care about it any more.
Also you should consider the OO principle of information hiding. Not everything that comes back from or goes to the database should be directly accessible.
There are several reasons to return objects:
Writing $myObject->property requires fewer "overhead" characters than $myArray['element']
Object can return data and functionality; arrays can contain only data.
Enable chaining: $myobject->getData()->parseData()->toXML();
Easier coding: IDE autocompletion can provide method and property hints for object.
In terms of performance, arrays are often faster than objects. In addition to performance, there are several reasons to use arrays:
The the functionality provided by the array_*() family of functions can reduce the amount of coding necessary in some cases.
Operations such as count() and foreach() can be performed on arrays. Objects do not offer this (unless they implement Iterator or Countable).
It's usually not going to be because of performance reasons. Typically, objects cost more than arrays.
For a lot of APIs, it probably has to do with the objects providing other functionality besides being a storage mechanism. Otherwise, it's a matter of preference and there is really no benefit to returning an object vs an array.
An array is just an index of values. Whereas an object contains methods which can generate the result for you. Sure, sometimes you can access an objects values directly, but the "right way to do it" is to access an objects methods (a function operating on the values of that object).
$obj = new MyObject;
$obj->getName(); // this calls a method (function), so it can decide what to return based on conditions or other criteria
$array['name']; // this is just the string "name". there is no logic to it.
Sometimes you are accessing an objects variables directly, this is usually frowned upon, but it happens quite often still.
$obj->name; // accessing the string "name" ... not really different from an array in this case.
However, consider that the MyObject class doesn't have a variable called 'name', but instead has a first_name and last_name variable.
$obj->getName(); // this would return first_name and last_name joined.
$obj->name; // would fail...
$obj->first_name;
$obj->last_name; // would be accessing the variables of that object directly.
This is a very simple example, but you can see where this is going. A class provides a collection of variables and the functions which can operate on those variables all within a self-contained logical entity. An instance of that entity is called an object, and it introduces logic and dynamic results, which an array simply doesn't have.
Most of the time objects are just as fast, if not faster than arrays, in PHP there isn't a noticeable difference. the main reason is that objects are more powerful than arrays. Object orientated programming allows you to create objects and store not only data, but functionality in them, for example in PHP the MySQLi Class allows you to have a database object that you can manipulate using a host of inbuilt functions, rather than the procedural approach.
So the main reason is that OOP is an excellent paradigm. I wrote an article about why using OOP is a good idea, and explaining the concept, you can take a look here: http://tomsbigbox.com/an-introduction-to-oop/
As a minor plus you also type less to get data from an object - $test->data is better than $test['data'].
I'm unfamiliar with word press. A lot of answers here suggest that a strength of objects is there ability to contain functional code. When returning an object from a function/API call it shouldn't contain utility functions. Just properties.
The strength in returning objects is that whatever lies behind the API can change without breaking your code.
Example: You get an array of data with key/value pairs, key representing the DB column. If the DB column gets renamed your code will break.
Im running the next test in php 5.3.10 (windows) :
for ($i = 0; $i < 1000000; $i++) {
$x = array();
$x['a'] = 'a';
$x['b'] = 'b';
}
and
for ($i = 0; $i < 1000000; $i++) {
$x = new stdClass;
$x->a = 'a';
$x->b = 'b';
}
Copied from http://atomized.org/2009/02/really-damn-slow-a-look-at-php-objects/comment-page-1/#comment-186961
Calling the function for 10 concurrent users and 10 times (for to obtain an average) then
Arrays : 100%
Object : 214% – 216% (2 times slower).
AKA, Object it is still painful slow. OOP keeps the things tidy however it should be used carefully.
What Wordpress is applying?. Well, both solutions, is using objects, arrays and object & arrays, Class wpdb uses the later (and it is the heart of Wordpress).
It follows the boxing and unboxing principle of OOP. While languages such as Java and C# support this natively, PHP does not. However it can be accomplished, to some degree in PHP, just not eloquently as the language itself does not have constructs to support it. Having box types in PHP could help with chaining, keeping everything object oriented and allows for type hinting in method signatures. The downside is overhead and the fact that you now have extra checking to do using the “instanceof†construct. Having a type system is also a plus when using development tools that have intellisense or code assist like PDT. Rather than having to google/bing/yahoo for the method, it exists on the object, and you can use the tool to provide a drop down.
Although the points made about objects being more than just data are valid since they are usually data and behaviour there is at least one pattern mentioned in Martin Fowler's "Patterns of Enterprise Application Architecture" that applies to this type of cenario in which you're transfering data from one system (the application behind the API) and another (your application).
Its the Data Transfer Object - An object that carries data between processes in order to reduce the number of method calls.
So if the question is whether APIs should return a DTO or an array I would say that if the performance cost is negligible then you should choose the option that is more maintainable which I would argue is the DTO option... but of course you also have to consider the skills and culture of the team that is developing your system and the language or IDE support for each of the options.

How to deal with the 'DataSet' data structure in php?

I'm trying to do a call to a soap webservice from php.
The webservice returns an ADO.NET DataSet structure. Are there any libraries in PHP that can deal with this sort of data structures? If so, what are they called and where can I find them? If not, feel free to give tips?
So far, I have this (using ActiveMQ and the NuSoap library):
/**
* Create a new service instance
* Provide ActiveMQ uri and the extended class name
**/
$client = new Client('tcp://localhost:61613?tcpNoDelay=false', 'test');
/**
* New service reference
**/
$service = new ServiceProxy($client, 'ServiceName');
/* Service call */
$result = $service->get_clients();
get_clients() is a method that does the actual service call and it gets the DataSet structure in return. How can I manipulate this return value?
In .NET for example, there is a DataSet class. The ADO.NET DataSet contains one or more ADO.NET DataTables which in their turn consist out of one or more ADO.NET DataColumns and DataRows which are returned in a collection (array).
A simple code example, where the DataSet only contains one DataTable, can be:
/**
* Here, the val variable will contain the data positioned in the
* first field of the first DataRow of the first DataTable
**/
string val = dataset.Table[0].Rows[0].ItemArray[0];
I want to do the same in PHP, but I need a helping hand.
I would suggest you try learning more about the structure of the data you are working with. It is most likely some form of JSON or XML, which can be manipulated through the large set of PHP xml and json handling libraries.
Your first step is to look at the data being returned by your request and identifying it's format. Microsoft documents the ADO.NET API's datasets in depth HERE at the MSDN library. This should help you to make sense of what you are seeing when you inspect the data. Some data providers allow you to access the same data in different formats, depending on a parameter or a family of similar functions. Do you have documentation for your providers API?
Next, after you have identified the format and deciphered the specifics of the datasets schema, You need a class to manipulate the data. If you are dealing with something that conforms to a published standard you can use something like simpleXML, JSON or DOMXPath.This class should store the data in protected member variables and provide methods to inspect, iterate, load, refresh, search and so forth. You should refer to the PHP manual's function reference for help here and write whatever functions you need. I would write it generically to handle any similar dataset and derive a class to expose exactly the data I need for a particular application.
Another approach would be to write a COM component in a .net language to access and manipulate the data and import it's functions through PHP's COM extension. I think I would go with this choice if the data format is a weird proprietary Microsofty one.
The third possible approach works only if you have programming access to the server. If you can adapt the service provider to comply with the specification, Microsoft has released an interoperability toolkit which is supposed to act as a bridge between .net services and PHP clients using a layer of proxy objects to expose the ado.net data to PHP scripts. They throw the word RESTful around alot, but I'm not really sure what they mean by that. Check out the OData SDK for PHP HERE at Microsoft's interoperability HQ (there is info here about PHP and other Microsoft platforms and products like Azure, silverlight, Bing, etc., as well). As I said though, I think you would need the data provider to emit data which conforms to their standard. Perhaps it already does. I can't tell without a schema! If so then this is your best bet.
Good Luck!

Is marshaling/ serialization in PHP as simple as serialize($var)?

here's a definition of marshaling from Wikipedia:
In computer science, marshalling
(similar to serialization) is the
process of transforming the memory
representation of an object to a data
format suitable for storage or
transmission. It is typically used
when data must be moved between
different parts of a computer program
or from one program to another.
I have always done data serialization in php via its serialize function, usually on objects or arrays. But how is wikipedia's definition of marshaling/serialization takes place in this serizalize() function?
What serialize doesn't do is transport class definitions. When unserializing an object, that object's class definition must be present (loaded from the code base), otherwise unserializing will fail. From the Wikipedia article you mention:
To "marshal" an object means to record its state and codebase(s) in such a way that when the marshalled object is "unmarshalled", a copy of the original object is obtained, possibly by automatically loading the class definitions of the object. You can marshal any object that is serializable or remote. Marshalling is like serialization, except marshalling also records codebases. Marshalling is different from serialization in that marshalling treats remote objects specially.
If I understand correctly, Serialize is definitely not 100% compatible with the definition of marshaling in that respect. I don't know a pre-defined mechanism that would do this in PHP. I guess you would have to combine the serialized data and all necessary class definitions into a package (a ZIP file for example).
Like Pekka mentioned above, PHP doesn't include the class definition, so it does not do marshaling. If the class for a serialized object is present, however, then the answer to your question is yes: serialization is as easy as serialize($abc).
The best way that I know of to take care of marshaling in PHP is to use a third party tool like Google Buffer Protocols or Facebook (Apache?) Thrift, which will serialize and marshal for you. Kind of a roundabout way of doing it (and as long as you have the class present, you don't need to marshal anyway), but they're probably the best solution to the problem.

Categories