ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm wondering how can I use pointers in PHP. I've experience in C with pointers and creating data structures like lists, linked lists (chains) and similar, and now I want to do these in PHP. The first thing I've read about references are that they ARE NOT pointers
I'm interested if PHP can do structs?
*sigh* Why all PHP learning books write only about making SQL queries and writing HTML
In PHP4 or 5 you would set up a class, which would give you an object. You can use the object like a struct, if that is what you want to do. You also can do indirection with PHP. The syntax is not nearly so elegant as C pointer syntax, but it is possible.
In PHP 5, everything is now passed-by-reference, which is good, because it makes making data structures simpler. In PHP4, when you would pass an object to a function, it only passed a value (a copy, essentially). So, any where you wanted to pass an object by reference, you had to use an & (ampersand), but, with PHP, it becamse pretty easy to accidently make a copy instead. The main point is, if you plan on supporting PHP4, it'll take a bit more work and be a bit uglier. PHP5 references are not pointers per se, no, but they are used in the way you expect. Anyways, here is part of a simple linked list in PHP5.
Code:
class LinkedList {
private $head = null;
private $tail = null;
private $size = 0;
public function __construct()
{
}
public function add($data)
{
$node = new Node($data);
if (is_null($this->tail)) {
$this->head = $this->tail = $data;
$this->size++;
return;
}
$this->tail->next = $node;
$node->prev = $this->tail;
$this->tail = $node;
$this->size++;
}
// ... more functionality goes here
}
class Node {
public $data = null;
public $prev = null;
public $next = null;
public function __construct($data = null)
{
$this->data = $data;
}
}
Quote:
Originally Posted by ivanatora
*sigh* Why all PHP learning books write only about making SQL queries and writing HTML
98% of the people who are programming with PHP only want to do that.
In the strict sense of the definition a reference is not a pointer, however it is a way of referring to another variable and so from a data structures view point it performs the task of a pointer. Many years ago I wrote some code in PHP illustrating how the basic data structures work, so it's possible, and yes I used the class approach suggested by the previous posters.
That looks correct to me, although I've not tried to run it! What I would say is that the constructor you are using is PHP4 syntax, in PHP5 they changed it to be __construct(), if you have access to PHP5 I'd use that because it has much better OO support.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.