Showing posts with label Self. Show all posts
Showing posts with label Self. Show all posts

Monday, December 22, 2008

php中self关键字说明

self一般指向当前类的静态方法和常量,用self::加方法名和常量名方式引用。
$this则是指向当前类的实例对象,用$this->加方法名和实例变量方式引用。
在一些参数为callback的方法里,可以用字符串'self'形式指向当前类,而不要直接用self,如call_user_func('self', $method)中。
另外self引用的总是当前类的方法和常量,子类调用父类的静态方法,其中的父类方法中的self仍是指向父类本身的,如果子类的同名方法覆盖了父类方法,则可以用parent::来引用父类方法。

<?php
interface AppConstants {
const FOOBAR = 'Hello, World.';
}

class Example implements AppConstants {
public function test() {
echo self :: FOOBAR;
}
}

$obj = new Example();
$obj->test(); // outputs "Hello, world."

class MyClass {
const NAME = 'Foo';

protected function myFunc() {
echo "MyClass::myFunc()\n";
}
static public function display() {
echo self :: NAME;
}
static public function getInstance() {
$instance = new self;
return $instance;
}
}

class ChildClass extends MyClass {
const NAME = 'Child';

// Override parent's definition
public function myFunc() {
// But still call the parent function
parent :: myFunc();
echo "ChildClass::myFunc()\n";
}
}

$class = new ChildClass();
$class->myFunc();


echo('Class constant: ');
ChildClass :: display();
echo('Object class: ');
echo(get_class(ChildClass :: getInstance()));
?>

== output ==
Hello, World.
MyClass::myFunc()
ChildClass::myFunc()
Class constant: Foo
Object class: MyClass
另外可以再参考一个php 5.3.0的运行时绑定(迟绑定)的用法,地址是:
http://cn2.php.net/oop5.late-static-bindings

Monday, January 07, 2008

WARNING DON’T LEAVE OUT THE DOT WHEN IT’S NEEDED [R4R page 186]

In one situation, you must use the full object-dot-message notation, even if you’re sending the message to the current self: when the method is a setter method—a method whose name ends with an equal sign. You have to do self.venue = "Town Hall" rather than venue = "Town Hall", if you want to call the method venue=. The reason is that Ruby always interprets the sequence: bareword = value as an assignment to a local variable. To call the method venue= on the current object, you need to include the explicit self. Otherwise, you’ll end up with a variable called venue and no call to the setter method.

Monday, July 02, 2007

Ruby attr_accessor使用


class Name
attr_accessor :name

def initialize
@name = ""
p @name.object_id
puts "\n"
end

def set_name(input_name)
name = input_name
self.name = input_name
end

def gets_name
p @name
p @name.object_id
p self.name
p self.name.object_id
end
end

a = Name.new
a.set_name('test')
a.gets_name
p a.name
p a.name.object_id

# >> 1647780
# >>
# >> "test"
# >> 1647790
# >> "test"
# >> 1647790
# >> "test"
# >> 1647790

puts "\n"
a.name = 'text'
a.gets_name
p a.name
p a.name.object_id

# >>
# >> "text"
# >> 1647680
# >> "text"
# >> 1647680
# >> "text"
# >> 1647680


self.name中的self是指当前的实例对象a,而self.name这个方法返回的则是对象a里的实例变量@name的值。
另:R4R第7章内容是关于ruby self的使用,说得非常详细,不同的作用域self代表的是不同的对象。
The default object (self) and scope In this chapter
■ The role of the current or default object, self
■ Scoping rules for variables and constants
■ Method access rules