# php魔术方法构成整个面向对象

__toString

class Person{
    public function __toString()
    {
        return '__toString方法必须加一个return';
    }
}
$p = new Person();
echo $p;
1
2
3
4
5
6
7
8

__clone 在我们项目中需要使用一个或多个对象,如果使用new关键词重新创建对象的话,再赋上相同的值,这样比较繁琐而且容易出错**

class Person
{
    var $name;
    var $age;

    public function __construct($name, $age)
    {
        $this->name = $name;
        $this->age = $age;
    }

    public function say()
    {
        echo 'name:' . $this->name . '<br>' . 'age:' . $this->age . '<br>';
    }
}

$test1 = new Person('xiaoming', '19');
echo $test1->say();

$test2 = clone $test1;
$test2->say();
//name:xiaoming
//age:19
//name:xiaoming
//age:19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

serialize()序列化 unserialize()反序列化 为了传输方便,可以把对象转化程二进制,等到达另一端的时候,再还原成原来的对象。 1、一个对象再网络中传输的时候需要将对象串形化 2、把对象写入文件或者数据库的时候用到串形化

__sleep()和__wakeup() 在序列化执行之前和重新生成对象的时候 __autolaod()自动加载 可以理解程按需加载 __autoload()是专门为很多类不存在设计的,很多框架利用这个实现了类文件的自动加载

function __autoload($classname)
{
    require_once $classname . 'php';
}

//当Myclass1不存在的时候,自动调用__autoload()函数,传入参数Myclass1;
$obj1 = new Myclass1();
$obj2 = new Myclass2();
1
2
3
4
5
6
7
8

# JS 构成整个面向对象

这里先看的是老版本的js

function Car() {
    console.log('这是一个Car!');
}
//php 是需要一个__construct才能执行
var test = new Car();//这是一个Car
1
2
3
4
5
function Car(color) {
    this.color = color;
}

Car.prototype.run = function () {
    console.log(this.color + ' run !');
}
var test = new Car('red');
test.run();//red run !
console.log(test);//Car {color: "red"} js 是一个面向原型链编程的语言
__proto__:是一个对象拥有的内置属性,是JS内部使用寻找原型链的属性。可以理解为它是一个指针,用于指向创建它的函数对象的原型对象prototype(即构造函数的prototype)。
__proto__ 下面有个construtor 还有通过原型prototype创建的方法run
1
2
3
4
5
6
7
8
9
10
11
12
function Car(color) {
        this.color = color;
        console.log('color', color);
    }

    Car.prototype.run = function () {
        console.log(this.color + ' run !');
    }

    var Cruze = function (color) {
        Car.call(this, color);//这个相当与php中parent::
    }
    Cruze.prototype = Car.prototype;

    var test = new Cruze('red');
    console.log(test);//这样父类会执行一次 并且输出的__proto__ -> constructor -> name = Car
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

完美实现继承的方法

function Car(color) {
    this.color = color;
    console.log('color', color);
}

Car.prototype.run = function () {
    console.log(this.color + ' run !');
}

var Cruze = function (color) {
    Car.call(this, color);//这个相当与php中parent::
}

//创建副本,拒绝__newCar = new Car();这样父类会执行两遍
var __newCar = Object.create(Car.prototype);
console.log(__newCar);//这个子类输出的是Car{}这个父类 constructor指向的还是Car
__newCar.constructor = Cruze;//修改constructor
Cruze.prototype = __newCar;
var result = new Cruze('blue');
console.log(result);
// Cruze {color: "blue"}
// color: "blue"
// __proto__: Car
// constructor: ƒ (color)
// arguments: null
// caller: null
// length: 1
// name: "Cruze"
// prototype: Car {constructor: ƒ}
// __proto__: ƒ ()
//     [[FunctionLocation]]: index.html?_ijt=vrdj…bgqtbjf41uu26v44:57
//     [[Scopes]]: Scopes[1]
// __proto__: Object
Cruze.prototype.gogo = function () {
    console.log('gogo');
}

var result2 = new Car('yellow')
console.log(result2);//父类没有被影响,并没有gogo方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# php简单数据库操作

<?php
header("Content-Type:text/html;charset=utf-8");
$dbms = "mysql";
$host = "localhost";
$dbName = "phplesson";
$user = "root";
$pass = "";
$dsn = "$dbms:host=$host;dbname=$dbName";
try {
    $dbh = new PDO($dsn, $user, $pass);
    $dbh -> query('set names utf-8');

    echo "连接成功!";
    $newstitle = $_REQUEST['newstitle'];
    $newsimg = $_REQUEST['newsimg'];
    $newscontent = $_REQUEST['newscontent'];
    $addtime = $_REQUEST['addtime'];
    $query = "INSERT INTO `news`(`newstitle`, `newsimg`, `newscontent`, `addtime`) VALUES ('" . $newstitle . "','" . $newsimg . "','" . $newscontent . "','" . $addtime . "')";

//    foreach ($dbh->query("SELECT*FROM `news`WHERE 1") as $row) {
//        print_r($row);
//    }
//    $query = "INSERT INTO `news`(`newstitle`, `newsimg`, `newscontent`, `addtime`) VALUES ('题1111  11目1','图片','内容','2015-09-01')";
//    $query = "DELETE FROM `news` WHERE 'newsid'=15";

    $res = $dbh->exec($query);
    echo "数据库操作成功,受影响的函数" . $res;
    $dbh = null;
} catch (PDOException $e) {
    die("Error" . $e . getMessage() . "</br>");
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<form action="mysqlpdo.php">
    <p>
        <label for="newstitle">新闻标题</label>
        <input type="text" id="newstitle" name="newstitle">
    </p>
    <p>
        <label for="newsimg">图片地址</label>
        <input type="text" id="newsimg" name="newsimg">
    </p>
    <p>
        <label for="newscontent">新闻内容</label><br>
        <textarea name="newscontent" id="newscontent" cols="30" rows="10"></textarea>
    </p>
    <p>
        <label for="addtime">新闻时间</label>
        <input type="date" id="addtime" name="addtime">
    </p>
    <p>
        <input type="submit" value="提交">
        <input type="reset" value="重置">
    </p>
</form>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
header("Content-type: text/html; charset=utf-8");
$userName = $_REQUEST['userName'];
if ($userName == 'userName') {
    echo json_encode(array('msg' => '登录成功', 'error' => '登录失败'));
} else {
    echo '登录失败';
}
?>
1
2
3
4
5
6
7
8
9