php 基础笔记 class
/***************************by
garcon1986********************************/
<?php
//简单示例
class SimpleClass
{
public $var = 'a default value';
public function displayVar(){
echo $this->var;
}
}
// create an object创建一个对象
$A = new SimpleClass;
//调用方法
$A -> displayVar();
echo '<p>';
//example2
class A{
function sjg(){
if(isset($this)){
echo '$this is defined.<br>\n';
echo get_class($this); //返回对象所属的类的名字
echo ")<br>";
}else {
echo "\$this is not defined.<br> \n";
}
}
}
class B{
function bar(){
A::sjg();
}
}
$a = new A();
$a -> sjg();
A::sjg();
$b = new B();
$b -> bar();
B::bar();
echo '<p>';
//example3
//创建一个实例
$instance = new SimpleClass();
//对象赋值
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = null;
var_dump($instance);
echo "<br>";
var_dump($reference);
echo "<br>";
var_dump($assigned);
echo "<p>";
//example4
//继承
class ExtendClass extends SimpleClass{
//redefine the parent method
function displayVar(){
echo "Extending class\n";
parent::displayVar();
}
}
$extended = new ExtendClass();
$extended->displayVar();
echo '<p>';
//autoload自动加载
function __autoload($class_name){
require_once $class_name.'.php';
}
//$obj = new MyClass1();
//$obj = new MyClass2();
//constructors
class BaseClass{
function __construct(){
print "In BaseClass constructor!\n";
}
}
class SubClass extends BaseClass{
function __construct(){
parent::__construct();
print "in SubClass constructor\n";
}
}
$obj = new BaseClass();
echo '<br>';
$obj->__construct();
echo '<br>';
$obj = new SubClass();
echo '<br>';
$obj->__construct();
echo '<p>';
//destructors
class MyDestructableClass{
function __construct(){
print "In constr
相关文档:
Google为全球主要城市提供了统一的天气预报数据存储格式,那就是XML。所有的开发者都可以利用自己喜欢的语言来解析XML获取所需城市的天气预报,本文将介绍利用PHP来获取我所在城市济南的天气预报。
原文见本人网站【PHP探路者】
原文链接:
PHP5 读取Google 天气预报XML API ......
php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charse ......
/***************************by
garcon1986********************************/
<?php
// -> 是指对象的方法或者属性
class Cart{
public function add_item($a,$b){
echo $a+$b.'<br>';
}
}
//$cart = new Cart; 两句意义相同
$cart = new Cart();
$cart->add_item("10", 1);
// =& ......
/***************************by
garcon1986********************************/
<?php
// variable name is sensitive
$var = "sjg";
$Var = "wl";
echo $var.' loves '.$Var.'<br>';
echo "$var, $Var<p>";
//naming conventions for variables
//$4site = 'not y ......
/***************************by
garcon1986********************************/
<?php
//example1
$makefoo = true;
bar();
if($makefoo){
function foo(){
echo "doesn't exist.<br>";
}
}
if($makefoo)foo();
function bar(){
echo "exist<br>";
}
//example2
funct ......