Jump to content

Recommended Posts

;D

 

pretty damn cool. makes me want to rewrite half of my code

 

<?php

class test{
public $_str;
public function __construct(){
	$this->x()->y();
}

public function x(){
	$this->_str .= 'x';
	return $this;
}

public function y(){
	$this->_str .= ' y';
	return $this;	
}
}

$n = new test();
echo $n->_str;
?>

I'm only saying this for the newer OOP programmers, be very careful when using this kind of syntax.  You must be certain that the functions you are chaining return the proper object types or you will run into problems!

does it work for static?

 

<?php

class test{
public $_str;

public static function x(){
	$self->_str .= 'x';
	return $self;
}

public static function y(){
	$self->_str .= ' y';
	return $self;	
}
}

$x = test::x()->y();
echo $x->_str;

?>

I rather doubt it, particularly in the way it's designed in the code you wrote. I'm sure you can chain static method calls, but not a mixture of static and non-static method calls in the same object/class. roopurt18's warning applies in this situation - you're expecting different datatypes than you're getting.

 

The problems I see with it:

 

1) You can't reference or modify non-static properties from within a static method.

2) I don't think $self is what you're looking for - you use $this from within instantiated objects, and the keyword "self" from within static elements.

3) When you use test::x()->y(), you're assuming that an object is returned from test::x(), which isn't possible until test is instantiated.

I rather doubt it, particularly in the way it's designed in the code you wrote. I'm sure you can chain static method calls, but not a mixture of static and non-static method calls in the same object/class. roopurt18's warning applies in this situation - you're expecting different datatypes than you're getting.

 

The problems I see with it:

 

1) You can't reference or modify non-static properties from within a static method.

2) I don't think $self is what you're looking for - you use $this from within instantiated objects, and the keyword "self" from within static elements.

3) When you use test::x()->y(), you're assuming that an object is returned from test::x(), which isn't possible until test is instantiated.

 

1. i thought that all public properties/methods were considered static, if not i could change it to "public static $_str;"

2&3. so returning $self isnt returning an object (i cant test it right now)?

 

can someone run

 

$x = test::x();

echo is_object($x);

 

on the last class that i posted to see what is returned? or better yet, var_dump($x)

 

 

Can somebody please explain to me the differance between this OOP chain thing and procedural nesting of functions?

 

It's pretty simple.  Let's say you have a class Manager that can return an instance of your DB object:

<?php
  $db = Manager->getDB();
  $db->query($sql);
?>

 

Since you know that the getDB method of Manager will return a DB object, you don't really need the $db variable.

<?php
  Manager->getDB()->query($sql);
  // Due to operator associativity, the above is executed as if it were:
  // (Manager->getDB())->query($sql)
  // The (Manager->getDB()) is representative of the $db var in the
  // first example
?>

 

You see this syntax more often in Javascript, Java, and C++.  For example, if you wanted the body element in Javascript:

  document.getElementsByTagName("body")[0]->appendChild(someNode);

 

I'm not sure about the implications in Java or Javascript, but if you were going to reuse the returned object multiple times I'd just store it in a variable.  Using the first example again:

<?php
  $db = Manager->getDB();
  $db->query($sql);
  echo $db->successMsg();
?>

 

I consider that a little more efficient (as far as PHP is concerned) than:

<?php
  Manager->getDB()->query($sql);
  echo Manager->getDB()->successMsg();
?>

 

The reason is that in the second example here we have to call Manager->getDB twice to get the same value; this is slightly inefficient, but will still run really fast.

 

However, in a language like C++ you don't have this problem.  You can return a const reference to the object (yes I'm aware you can return references in PHP) and you can also declare the function as inline.

 

Hope that helps to clarify!

1. i thought that all public properties/methods were considered static, if not i could change it to "public static $_str;"

2&3. so returning $self isnt returning an object (i cant test it right now)?

 

can someone run

 

$x = test::x();

echo is_object($x);

 

on the last class that i posted to see what is returned? or better yet, var_dump($x)

 

 

 

Properties/methods in a class are, by default, non-static and require instantiation to be usable. Otherwise the 'static' declaration would be absolutely useless, no? :)

 

And I was mistaken - test::x() does indeed return an object, but it isn't an object of type test. Rather, it's an object of type "stdClass", which I'm assuming is the default "generic" class that PHP uses when dealing with static class members.

 

object(stdClass)#1 (1) {
  ["_str"]=>
  string(1) "x"
}

thanks neylitalo. I thought they they defaulted static and only threw a warning if you were in strict mode. I thought that the static was introduced so that you know how to code it the correct way, while the incorrect[legacy] way was still available. because you can call methods statically without the static declaration (we do it all the time at work, well they do it, i know better).

 

and from your explanation I now see that it returns the stdClass instance and not an instance of test. cool stuff

Without trying to sound condescending, this is pretty basic stuff..

 

It is, I use it ALL the time in my js programming for chaining events/effects. However, In all of my php research and reading and coding, I've never seen it mentioned. I just never thought about it and I do not have a background in any fundamental languages where these things are the norm.

Can somebody please explain to me the differance between this OOP chain thing and procedural nesting of functions?

 

It's pretty simple.  Let's say you have a class Manager that can return an instance of your DB object:

<?php
  $db = Manager->getDB();
  $db->query($sql);
?>

 

Since you know that the getDB method of Manager will return a DB object, you don't really need the $db variable.

<?php
  Manager->getDB()->query($sql);
  // Due to operator associativity, the above is executed as if it were:
  // (Manager->getDB())->query($sql)
  // The (Manager->getDB()) is representative of the $db var in the
  // first example
?>

 

You see this syntax more often in Javascript, Java, and C++.  For example, if you wanted the body element in Javascript:

  document.getElementsByTagName("body")[0]->appendChild(someNode);

 

I'm not sure about the implications in Java or Javascript, but if you were going to reuse the returned object multiple times I'd just store it in a variable.  Using the first example again:

<?php
  $db = Manager->getDB();
  $db->query($sql);
  echo $db->successMsg();
?>

 

I consider that a little more efficient (as far as PHP is concerned) than:

<?php
  Manager->getDB()->query($sql);
  echo Manager->getDB()->successMsg();
?>

 

The reason is that in the second example here we have to call Manager->getDB twice to get the same value; this is slightly inefficient, but will still run really fast.

 

However, in a language like C++ you don't have this problem.  You can return a const reference to the object (yes I'm aware you can return references in PHP) and you can also declare the function as inline.

 

Hope that helps to clarify!

Thanks but I don't understand sorry.

How exactly is this different from nesting functions in procedural? Like echo(mysql_result(mysql_query("select text from table"),0));

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.